Filtering with multiple conditions in R is accomplished using with filter() function in dplyr package. Let’s see how to apply filter with multiple conditions in R with an example.
Let’s first create the dataframe.
### Create Data Frame df1 = data.frame(Name = c('George','Andrea', 'Micheal','Maggie','Ravi','Xien','Jalpa'), Grade_score=c(4,6,2,9,5,7,8), Mathematics1_score=c(45,78,44,89,66,49,72), Science_score=c(56,52,45,88,33,90,47)) df1
So the resultant dataframe will be
Filtering with multiple conditions in R:
Filtering with 2 columns using or condition.
library(dplyr) result_or <- df1 %>% filter(Mathematics1_score>45 | Science_score>45) result_or
so the filtered dataframe is
Filtering with multiple conditions in R:
Filtering with 2 columns using and condition.
library(dplyr) result_and <- df1 %>% filter(Mathematics1_score>45 & Science_score>45) result_and
so the filtered dataframe is