# Filter Using Multiple Conditions in R

**Data Science Tutorials**, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

The post Filter Using Multiple Conditions in R appeared first on Data Science Tutorials

Filter Using Multiple Conditions in R, Using the dplyr package, you can filter data frames by several conditions using the following syntax.

How to draw heatmap in r: Quick and Easy way – Data Science Tutorials

**Method 1: Using OR, filter by many conditions.**

library(dplyr) df %>% filter(col1 == 'A' | col2 > 50)

**Method 2: Filter by Multiple Conditions Using AND**

library(dplyr) df %>% filter(col1 == 'A' & col2 > 80)

With the following data frame in R, the following example explains how to apply these methods in practice.

glm function in r-Generalized Linear Models – Data Science Tutorials

Let’s create a data frame

df <- data.frame(team=c('P1', 'P2', 'P3', 'P4', 'P5', 'P6', 'P7', 'P8'), points=c(110, 120, 80, 16, 105, 185, 112, 112), assists=c(133, 128, 131, 139, 134,55,66,135), rebounds=c(18, 18, 14, 13, 12, 15, 17, 12))

Now we can view the data frame

df team points assists rebounds 1 P1 110 133 18 2 P2 120 128 18 3 P3 80 131 14 4 P4 16 139 13 5 P5 105 134 12 6 P6 185 55 15 7 P7 112 66 17 8 P8 112 135 12

## Method 1: Multiple Conditions Filter Using the OR

The code below demonstrates how to use the or (|) operator to filter the data frame by rows that satisfy one or more conditions.

Descriptive statistics vs Inferential statistics: Guide – Data Science Tutorials

library(dplyr)

Find rows where the team is equivalent to ‘P1’ or the points total exceeds 90.

df %>% filter(team == 'P1' | points > 90) team points assists rebounds 1 P1 110 133 18 2 P2 120 128 18 3 P5 105 134 12 4 P6 185 55 15 5 P7 112 66 17 6 P8 112 135 12

The only rows that are returned are those in which the team is equal to ‘P1’ or the points total exceeds 90.

In the filter function, we can use as many “or” operators as we want.

library(dplyr)

Find rows where the team is equivalent to ‘P1’ or ‘P2’, or where the points are fewer than 90.

Calculate the P-Value from Chi-Square Statistic in R.Data Science Tutorials

df %>% filter(team == 'P1' | team == 'P2' | points < 90) team points assists rebounds 1 P1 110 133 18 2 P2 120 128 18 3 P3 80 131 14 4 P4 16 139 13

## Method 2: Filter by Multiple Conditions Using AND

The following code demonstrates how to use the and (&) operator to filter the data frame by rows that satisfy a number of criteria.

library(dplyr)

Find rows where the team is ‘P1’ and the points are larger than 90.

df %>% filter(team == 'P1' & points > 90) team points assists rebounds 1 P1 110 133 18

Only one entry in the filter function matched both conditions.

In the filter function, we may also use as many “and” operators as we want.

How to compare variances in R – Data Science Tutorials

library(dplyr)

Where the team is equivalent to ‘P1’, the points are greater than 100, and the assists are less than 150.

df %>% filter(team == 'P1' & points > 100 & assists < 150) team points assists rebounds 1 P1 110 133 18

The post Filter Using Multiple Conditions in R appeared first on Data Science Tutorials

**leave a comment**for the author, please follow the link and comment on their blog:

**Data Science Tutorials**.

R-bloggers.com offers

**daily e-mail updates**about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.