I have a data frame called fruits where each row has up to 3 fruits with their corresponding color. Color1 goes with Fruit1, Color2 with Fruit2, and Color3 with Fruit3.
Color1 Color2 Color3 Fruit1 Fruit2 Fruit3
1 red green green apple mango kiwi
2 yellow green red banana plum mango
3 green red grape apple
4 yellow apple
Using dplyr, I can return the rows that contain apples (1, 3 and 4). And I can return the rows with red (1, 2 and 3).
red <- filter_at(fruits, vars(Color1:Color3), any_vars(. == "red"))
apple <- filter_at(fruits, vars(Fruit1:Fruit3), any_vars(. == "apple"))
But how do I return only red apples, i.e. just the first row (Color1 = red, Fruit1 = apple) and the third (Color2 = red, Fruit2 = apple)?
Thanks.
p.s. Here is the code for table
Color1 <- c("red", "yellow", "green", "yellow")
Color2 <- c("green", "green", "red", "")
Color3 <- c("green", "red", "", "")
Fruit1 <- c("apple", "banana", "grape", "apple")
Fruit2 <- c("mango", "plum", "apple", "")
Fruit3 <- c("kiwi", "mango", "", "")
fruits <- data.frame (Color1, Color2, Color3, Fruit1, Fruit2, Fruit3)