Skip to content

Data Science Tutorials

For Data Science Learners

  • How to Rank by Group in R?
    How to Rank by Group in R? R
  • stacked barplot in R
    Stacked Barplot in R R
  • pheatmap function in R
    The pheatmap function in R R
  • How to plot categorical data in R
    Plot categorical data in R R
  • How to Find Quartiles in R
    How to Find Quartiles in R? R
  • Hypothesis Testing in R Programming
    Hypothesis Testing in R Programming R
  • Best Git Books R
  • How to Implement the Sklearn Predict Approach
    How to Implement the Sklearn Predict Approach? R
Filter Using Multiple Conditions in R

Filter Using Multiple Conditions in R

Posted on June 16June 11 By Admin No Comments on Filter Using Multiple Conditions in R

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

Check your inbox or spam folder to confirm your subscription.

R Tags:dplyr

Post navigation

Previous Post: What is the best way to filter by row number in R?
Next Post: How to Use “not in” operator in Filter

Related Posts

  • Pattern Mining Analysis in R-With Examples R
  • Algorithm Classifications in Machine Learning
    Algorithm Classifications in Machine Learning Machine Learning
  • How to copy files in R
    How to copy files in R R
  • Group By Minimum in R
    Group By Minimum in R R
  • Linear Interpolation in R
    Linear Interpolation in R-approx R
  • Aggregate daily data to monthly and yearly in R
    Aggregate daily data to monthly and yearly in R R

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • Best Prompt Engineering Books
  • Understanding Machine Learning and Data Science
  • Best Git Books
  • Top 5 Books to Learn Data Engineering
  • Mastering R Programming for Data Science: Tips and Tricks
  • About Us
  • Contact
  • Disclaimer
  • Privacy Policy

https://www.r-bloggers.com

  • YouTube
  • Twitter
  • Facebook
  • Course
  • Excel
  • Machine Learning
  • Opensesame
  • R
  • Statistics

Check your inbox or spam folder to confirm your subscription.

  • pheatmap function in R
    The pheatmap function in R R
  • How to Add Superscripts and Subscripts to Plots in R?, The basic syntax for adding superscripts or subscripts to charts in R is as follows:
    How to Add Superscripts and Subscripts to Plots in R? R
  • Is R or Python Better for Data Science in Bangalore
    Is R or Python Better for Data Science in Bangalore R
  • How to Create Summary Tables in R
    How to Create Summary Tables in R R
  • Hypothesis Testing Examples
    Hypothesis Testing Examples-Quick Overview Statistics
  • Return the corresponding value of Cauchy density in R
    Return the corresponding value of Cauchy density in R R
  • How to compare variances in R
    How to compare variances in R R
  • Count Observations by Group in R
    Count Observations by Group in R R

Privacy Policy

Copyright © 2025 Data Science Tutorials.

Powered by PressBook News WordPress theme