4.5 C
London
Thursday, December 19, 2024
HomeTidyverse in Rdplyr in RHow to Filter by Multiple Conditions Using dplyr

How to Filter by Multiple Conditions Using dplyr

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

You can use the following syntax to filter data frames by multiple conditions using the dplyr 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 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.

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

library(dplyr)

#filter where team is equal to 'A' and points > 89 and assists 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 here.

Additional Resources

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

How to Filter by Date Using dplyr
How to Filter for Unique Values Using dplyr
How to Filter by Row Number Using dplyr

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories