I'm trying to flag rows that have values that match elements in 2 external vectors. I need these values to match, meaning it can't be any case, but something where both conditions are met (i.e. not using an %in% operator).
Here's what I've tried:
widths <- c(1.2, 1.7, 1.8, 1.8, 1.9)
species <- c(rep("versicolor", 3), rep("virginica", 2))
iris_flagged <- iris %>%
filter(Species != "setosa") %>%
mutate(flag = ifelse((Petal.Width == widths & Species == species), "check", "")) %>%
arrange(Species, Petal.Width) #to help with visualization
But when I run this some flags are missed. If you look at the data you'll see that rows 14, 50, 57, 59, 61:64, and 66:71 are missing flags.
A clunky, non-robust method could look like this, but I really want to make it robust and use these vectors to my advantage.
iris_flagged2 <- iris %>%
filter(Species != "setosa") %>%
mutate(flag = ifelse((Species == "versicolor" & Petal.Width == 1.2 |
Species == "versicolor" & Petal.Width == 1.7 |
Species == "versicolor" & Petal.Width == 1.8 |
Species == "virginica" & Petal.Width == 1.8 |
Species == "virginica" & Petal.Width == 1.9),
"check", "")) %>%
arrange(Species, Petal.Width)
Thanks for your help. I tried to search but wasn't sure how to phrase my question.