Filtering R data-frame with multiple conditions

+1 vote
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
Mar 26, 2018 in Data Analytics by coldcode
• 2,090 points
87,144 views

3 answers to this question.

+2 votes

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.

answered Mar 26, 2018 by Bharani
• 4,660 points
very cool thanks
Thank you!!!!
0 votes
librarry(dplyr)

dataset >%> filter((a ==1,b>15 ) | (a==2,b>5))
answered Aug 8, 2019 by anonymous
0 votes

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.

answered Dec 11, 2020 by MD
• 95,460 points

Related Questions In Data Analytics

0 votes
1 answer

How to order data frame rows according to vector with specific order using R?

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

answered Apr 30, 2018 in Data Analytics by Sahiti
• 6,370 points
7,460 views
0 votes
1 answer

How to filter a data frame with dplyr and tidy evaluation in R?

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

answered May 17, 2018 in Data Analytics by DataKing99
• 8,250 points
1,895 views
0 votes
1 answer

create data.frame with random numbers in R

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

answered Sep 24, 2019 in Data Analytics by Debasmita Das
3,098 views
0 votes
1 answer

R program: Filter and re-arrange data based on multiple conditions

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

answered Jul 27, 2020 in Data Analytics by MD
• 95,460 points
1,477 views
0 votes
1 answer

Big Data transformations with R

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

answered Dec 18, 2017 in Data Analytics by Sudhir
• 1,570 points
1,030 views
+1 vote
1 answer

How to convert a list of vectors with various length into a Data.Frame?

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

answered Apr 4, 2018 in Data Analytics by DeepCoder786
• 1,720 points
1,659 views
0 votes
1 answer

How to spilt a column of a data frame into multiple columns

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

answered Apr 9, 2018 in Data Analytics by DeepCoder786
• 1,720 points
1,773 views
+1 vote
2 answers

How to sort a data frame by columns in R?

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

answered Aug 21, 2019 in Data Analytics by anonymous
• 33,030 points
1,810 views
+4 votes
11 answers

Creating an empty data.frame with only column names - R

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

answered Dec 11, 2020 in Data Analytics by MD
• 95,460 points
108,022 views
0 votes
1 answer

Converting R data-frame to h2o object

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

answered Apr 3, 2018 in Data Analytics by Bharani
• 4,660 points
2,562 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP