mardi 2 avril 2019

subset data only if observations of 2 variables match with each other in R

I have data like so:

a <- c("blue", "red", "green", "blue","cyan")
b <- c("red","red","green","blue", "orange")
df <- data.frame(a,b)
df

      a      b
1  blue    red
2   red    red
3 green  green
4  blue   blue
5  cyan orange

I am trying to subset the rows if blue and red match with each other and watch with themselves.

I am trying following code but there are still some other colors that are matching one of these colours when I see check them using table function.

 sub <- df[df$a %in% c("blue", "red" & df$b %in% c("blue","red"), ]
 table(sub$a, sub$b)

It is very tricky for me. How can I tell R to subset only if blue and red match with each other and watch with themselves?

Desired output is:

      a      b
1  blue    red
2   red    red
3  blue   blue

Thanks in advance!

Aucun commentaire:

Enregistrer un commentaire