user10072460
user10072460

Reputation:

Sorting data with conditions for multiple columns in R

I have the following:

df1<-read.table(text=" Id	Item	Group	Hard
Id	Item	Group	Hard
12	B12	A	Y
14	B6	B	N
17	B5	D	Y
33	B10	D	Y
44	B12	D	N
55	B6	D	Y
75	B5	D	Y
44	B10	A	Y
33	B12	D	N
21	B6	A	Y
16	B12	A	N
",header=TRUE)

I want to match my data using the ids. I want to get B12 if they are "Y" and "N" in the Hard column, I want to get B6 if they are only "Y" in hard Column.

I want to get the following table:

id	Item	Group	Hard
12	B12	A	Y
44	B12	D	N
55	B6	D	Y
33	B12	D	N
21	B6	A	Y
16	B12	A	N

I understand I need to tell you my effort, But I have searched but I could not find any solution. Thanks for your help.

Upvotes: 3

Views: 83

Answers (1)

akrun
akrun

Reputation: 887971

As there are only 'Y', 'N', in 'Hard' column, the first condition can be only on the 'Item' column and the second condition can use the 'Y' in 'Hard'

subset(df1, Item == 'B12'| (Item == 'B6' & Hard == 'Y'))
#    Id Item Group Hard
#2  12  B12     A    Y
#6  44  B12     D    N
#7  55   B6     D    Y
#10 33  B12     D    N
#11 21   B6     A    Y
#12 16  B12     A    N

With dplyr, change the subset to filter

library(dplyr)
filter(df1, Item == 'B12'| (Item == 'B6' & Hard == 'Y'))

Upvotes: 2

Related Questions