Skip to content

Data Science Tutorials

  • Home
  • R
  • Statistics
  • Course
  • Machine Learning
  • Guest Blog
  • Contact
  • About Us
  • Toggle search form
  • Remove Rows from the data frame in R
    Remove Rows from the data frame in R R
  • Applications of Data Science in Education
    Applications of Data Science in Education Machine Learning
  • How to Standardize Data in R
    How to Standardize Data in R? R
  • How to Count Distinct Values in R
    How to Count Distinct Values in R R
  • one-sample-proportion-test-in-r
    One sample proportion test in R-Complete Guide R
  • Two-Way ANOVA Example in R
    How to perform a one-sample t-test in R? R
  • How to Recode Values in R
    How to Recode Values in R R
  • How to Create Summary Tables in R
    How to Create Summary Tables in R R
Subsetting with multiple conditions in R

Subsetting with multiple conditions in R

Posted on May 19May 19 By Jim No Comments on Subsetting with multiple conditions in R
Tweet
Share
Share
Pin

Subsetting with multiple conditions in R, The filter() method in the dplyr package can be used to filter with many conditions in R.

With an example, let’s look at how to apply a filter with several conditions in R.

Let’s start by making the data frame.

df<-data.frame(Code = c('A','B', 'C','D','E','F','G'),
                 Score1=c(44,46,62,69,85,77,68),
                score2=c(35,78,45,89,67,49,70),
                 score3=c(57,62,55,88,43,90,57))
df

As a result, the final data frame will be

Code score1 score2 score3
1    A     44     35     57
2    B     46     78     62
3    C     62     45     55
4    D     69     89     88
5    E     85     67     43
6    F     77     49     90
7    G     68     70     57

Online Course R Statistics: Statistics with R »

Subsetting with multiple conditions in R

Using the or condition to filter two columns.

Let’s load dpyr package first,

library(dplyr)
result <- df%>% filter(score>50 | score2>55)
result

as a result, the filtered data frame

   Code score1 score2 Score3
1    B     46     78     62
2    C     62     45     55
3    D     69     89     88
4    E     85     67     43
5    F     77     49     90
6    G     68     70     57

Filtering with multiple conditions in R:

Using and condition to filter two columns.

library(dplyr)
df2 <- df %>% filter(score1>45 & score2>45)
df2

so the filtered data frame is

   Code score1 score2 Score3
1    B     46     78     62
2    D     69     89     88
3    E     85     67     43
4    F     77     49     90
5    G     68     70     57

Best Books to Learn R Programming – Data Science Tutorials

Check your inbox or spam folder to confirm your subscription.

Tweet
Share
Share
Pin
R Tags:filter, subset

Post navigation

Previous Post: How to draw heatmap in r: Quick and Easy way
Next Post: Sorting in r: sort, order & rank R Functions

Related Posts

  • gganatogram Plot in R
    How to create Anatogram plot in R R
  • How to put margins on tables or arrays in R?
    How to put margins on tables or arrays in R? R
  • Get the first value in each group in R
    Get the first value in each group in R? R
  • display the last value of each line in ggplot
    How to add labels at the end of each line in ggplot2? R
  • How to change the column positions in R?
    How to change the column positions in R? R
  • How to Calculate Ratios in R
    How to Calculate Ratios in R R

Leave a Reply Cancel reply

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

  • About Us
  • Contact
  • Disclaimer
  • Guest Blog
  • Privacy Policy
  • YouTube
  • Twitter
  • Facebook
  • Tips for Data Scientist Interview Openings
  • What is Epoch in Machine Learning?
  • Dynamic data visualizations in R
  • How Do Machine Learning Chatbots Work
  • Convex optimization role in machine learning

Check your inbox or spam folder to confirm your subscription.

  • Sampling from the population in R
  • Two of the Best Online Data Science Courses for 2023
  • Process of Machine Learning Optimisation?
  • ggplot2 scale in R (grammar for graphics)
  • ggplot aesthetics in R (Grammer of graphics)
  • Is R or Python Better for Data Science in Bangalore
    Is R or Python Better for Data Science in Bangalore R
  • Changing the Font Size in Base R Plots
    Changing the Font Size in Base R Plots R
  • How to Rank by Group in R?
    How to Rank by Group in R? R
  • sorting in r
    Sorting in r: sort, order & rank R Functions R
  • Error in solve.default(mat)  Lapack routine dgesv system is exactly singular
    Error in solve.default(mat) :  Lapack routine dgesv: system is exactly singular: U[2,2] = 0 R
  • ggdogs on ggplot2
    ggdogs on ggplot2 R
  • Algorithm Classifications in Machine Learning
    Algorithm Classifications in Machine Learning Machine Learning
  • Augmented Dickey-Fuller Test in R
    Augmented Dickey-Fuller Test in R R

Copyright © 2023 Data Science Tutorials.

Powered by PressBook News WordPress theme