Skip to content

Data Science Tutorials

  • Home
  • R
  • Statistics
  • Course
  • Machine Learning
  • Guest Blog
  • Contact
  • About Us
  • Toggle search form
  • A Side-by-Side Boxplot in R
    A Side-by-Side Boxplot in R: How to Do It R
  • How to create Sankey plot in R
    How to create a Sankey plot in R? R
  • Random Forest Machine Learning
    Random Forest Machine Learning Introduction R
  • How to Use Italic Font in R
    How to Use Italic Font in R R
  • How to Join Multiple Data Frames in R
    How to Join Multiple Data Frames in R R
  • Triangular Distribution in R
    Triangular Distribution in R R
  • What is the best way to filter by row number in R?
    What is the best way to filter by row number in R? R
  • How to Join Data Frames for different column names in R
    How to Join Data Frames for different column names in R R
Subsetting with multiple conditions in R

Subsetting with multiple conditions in R

Posted on May 19May 19 By Jim No Comments on Subsetting with multiple conditions in R
Tweet
Share
Share
Pin

Subsetting with multiple conditions in R, The filter() method in the dplyr package can be used to filter with many conditions in R.

With an example, let’s look at how to apply a filter with several conditions in R.

Let’s start by making the data frame.

df<-data.frame(Code = c('A','B', 'C','D','E','F','G'),
                 Score1=c(44,46,62,69,85,77,68),
                score2=c(35,78,45,89,67,49,70),
                 score3=c(57,62,55,88,43,90,57))
df

As a result, the final data frame will be

Code score1 score2 score3
1    A     44     35     57
2    B     46     78     62
3    C     62     45     55
4    D     69     89     88
5    E     85     67     43
6    F     77     49     90
7    G     68     70     57

Online Course R Statistics: Statistics with R »

Subsetting with multiple conditions in R

Using the or condition to filter two columns.

Let’s load dpyr package first,

library(dplyr)
result <- df%>% filter(score>50 | score2>55)
result

as a result, the filtered data frame

   Code score1 score2 Score3
1    B     46     78     62
2    C     62     45     55
3    D     69     89     88
4    E     85     67     43
5    F     77     49     90
6    G     68     70     57

Filtering with multiple conditions in R:

Using and condition to filter two columns.

library(dplyr)
df2 <- df %>% filter(score1>45 & score2>45)
df2

so the filtered data frame is

   Code score1 score2 Score3
1    B     46     78     62
2    D     69     89     88
3    E     85     67     43
4    F     77     49     90
5    G     68     70     57

Best Books to Learn R Programming – Data Science Tutorials

Check your inbox or spam folder to confirm your subscription.

Tweet
Share
Share
Pin
R Tags:filter, subset

Post navigation

Previous Post: How to draw heatmap in r: Quick and Easy way
Next Post: Sorting in r: sort, order & rank R Functions

Related Posts

  • Data Science Challenges in R Programming Language
    Data Science Challenges in R Programming Language Machine Learning
  • How to put margins on tables or arrays in R?
    How to put margins on tables or arrays in R? R
  • Cumulative Sum calculation in R
    Cumulative Sum calculation in R R
  • How to Label Outliers in Boxplots in ggplot2
    How to Label Outliers in Boxplots in ggplot2? R
  • How to Join Data Frames for different column names in R
    How to Join Data Frames for different column names in R R
  • droplevels in R with examples
    droplevels in R with examples R

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • About Us
  • Contact
  • Disclaimer
  • Guest Blog
  • Privacy Policy
  • YouTube
  • Twitter
  • Facebook
  • Top 7 Skills Required to Become a Data Scientist
  • Learn Hadoop for Data Science
  • How Do Online Criminals Acquire Sensitive Data
  • Top Reasons To Learn R in 2023
  • Linear Interpolation in R-approx

Check your inbox or spam folder to confirm your subscription.

 https://www.r-bloggers.com
  • Error in rbind(deparse.level ...) numbers of columns of arguments do not match
    Error in rbind(deparse.level …) numbers of columns of arguments do not match R
  • How to do Pairwise Comparisons in R?
    How to do Pairwise Comparisons in R? R
  • How to create a ggalluvial plot in r
    How to create a ggalluvial plot in R? R
  • How to Recode Values in R
    How to Recode Values in R R
  • How to Use Italic Font in R
    How to Use Italic Font in R R
  • How to Scale Only Numeric Columns in R
    How to Scale Only Numeric Columns in R R
  • display the last value of each line in ggplot
    How to add labels at the end of each line in ggplot2? R
  • Credit Card Fraud detection in R
    Credit Card Fraud Detection in R R

Copyright © 2023 Data Science Tutorials.

Powered by PressBook News WordPress theme