We wish to filter rows that satisfy a logical combination of multiple conditions.
We will cover the two most common scenarios:
We wish to filter rows that meet two (or more) conditions.
In this example, we wish to filter rows of the data frame df
where the numeric column col_1
is greater than 5 and where the string column col_2
has a value that contains the substring ‘token’
.
df_2 = df %>%
filter(col_1 > 5, str_detect(col_2, 'token'))
Here is how this works:
&
we can pass a list of comma separated conditions to filter()
. This greatly simplifies logical expression construction.filter()
retains all rows that satisfy the given conditions. In other words, to be included in the output, a row must evaluate to TRUE
for all conditions.We wish to filter rows that meet any one of two (or more) conditions.
In this example, we wish to filter rows of the data frame df
where the numeric column col_1
is greater than 5 or where the string column col_2
has a value that contains the substring ‘token’
.
df = df %>%
filter(col_1 > 5 | str_detect(col_2, 'token'))
Here is how this works:
|
operator.