How do you filter a data frame by multiple conditions using dplyr

To filter a data frame by multiple conditions using dplyr, use the filter() function and specify the various conditions within the parentheses. The syntax is: filter(data_frame, condition1, condition2, …). Each condition must be separated by a comma, and can be any logical statement or operator that evaluates to TRUE or FALSE. For instance, to filter a data frame by two conditions, use filter(data_frame, condition1 == TRUE, condition2 == FALSE). This will return the subset of the data frame that meets those criteria.


You can use the following syntax to filter data frames by multiple conditions using the library:

Method 1: Filter by Multiple Conditions Using OR

library(dplyr)

df %>%
  filter(col1 == 'A' | col2 > 90)

Method 2: Filter by Multiple Conditions Using AND

library(dplyr)

df %>%
  filter(col1 == 'A' & col2 > 90)

The following example shows how to use these methods in practice with the following data frame in R:

#create data frame
df <- data.frame(team=c('A', 'A', 'B', 'B', 'C'),
                 points=c(99, 90, 86, 88, 95),
                 assists=c(33, 28, 31, 39, 34),
                 rebounds=c(30, 28, 24, 24, 28))

#view data frame
df

  team points assists rebounds
1    A     99      33       30
2    A     90      28       28
3    B     86      31       24
4    B     88      39       24
5    C     95      34       28

Method 1: Filter by Multiple Conditions Using OR

The following code shows how to use the or ( | ) operator to filter the data frame by rows that meet one of multiple conditions:

library(dplyr)

#filter for rows where team is equal to 'A' or points is greater than 90
df %>%
  filter(team == 'A' | points > 90)

  team points assists rebounds
1    A     99      33       30
2    A     90      28       28
3    C     95      34       28

The only rows returned are those where the team is equal to ‘A’ or where points is greater than 90.

Note that we can use as many “or” operators as we’d like in the filter function:

library(dplyr)

#filter for rows where team is equal to 'A' or 'C' or points is less than 89
df %>%
  filter(team == 'A' | team == 'C' | points > 90)

  team points assists rebounds
1    A     99      33       30
2    A     90      28       28
3    B     86      31       24
4    C     95      34       28

Method 2: Filter by Multiple Conditions Using AND

The following code shows how to use the and ( & ) operator to filter the data frame by rows that meet several conditions:

library(dplyr)

#filter for rows where team is equal to 'A' and points is greater than 90
df %>%
  filter(team == 'A' & points > 90)

  team points assists rebounds
1    A     99      33       30

Only one row met both conditions in the filter function.

library(dplyr)

#filter where team is equal to 'A' and points > 89 and assists < 30
df %>%
  filter(team == 'A' & points > 89 & assists < 30)

  team points assists rebounds
1    A     90      28       28

Note: You can find the complete documentation for the dplyr filter() function .

The following tutorials explain how to perform other common operations in dplyr:

x