218/filtering-r-data-frame-with-multiple-conditions

a b 1 30 1 10 1 8 2 10 2 18 2 5

I have this data-set with me, where column 'a' is of factor type with levels '1' and '2'. Column 'b' has random whole numbers. Now, i would want to filter this data-frame such that i only get values more than 15 from 'b' column where 'a=1' and get values greater 5 from 'b' where 'a==2'

So, i would want the output to be like this:

a b 1 30 2 10 2 18

You can use the 'filter' function from 'dplyr' package to solve this:

library(dplyr) filter(df, (a==1 & b>15) | (a==2 & b>5))

Become a proficient Data Analyst with our comprehensive Data Analyst Course.

Hi,

you can use the with function in R as shown below.

with(df, df[ (x==1 & y>15) | (x==2 & y>5), ]) x y 1 1 30 4 2 10 5 2 18

Here you can use all possible conditions.

You can try using match: data <- data.frame(alphabets=letters[1:4], ...READ MORE

Requires the use of map_df to run each model, ...READ MORE

First create a matrix with random numbers ...READ MORE

Hi, You need to create your own customized ...READ MORE

Dear Koushik, Hope you are doing great. You can ...READ MORE

We can easily use this command as.data.frame(lapply(d1, "length< ...READ MORE

it is easily achievable by using "stringr" ...READ MORE

You can use dplyr function arrange() like ...READ MORE

Hi, You need to create a data frame ...READ MORE

All you have to do is run ...READ MORE

OR

At least 1 upper-case and 1 lower-case letter

Minimum 8 characters and Maximum 50 characters

Already have an account? Sign in.