I want to filter the rows of a data frame that match my vector of strings. Somehow i dont get the logic behind this:
fruits <- tibble(fruit=c("apple", "apple", "banana", "orange", "kiwi"))
# A tibble: 5 x 1
fruit
<chr>
1 apple
2 apple
3 banana
4 orange
5 kiwi
filter by vector c("a", "w")
, leads to the following output. why are kiwi
and orange
not filtered, why is apple
only filtered once?
fruits %>%
filter(str_detect(fruit, c("a", "w")))
fruit
# A tibble: 2 x 1
fruit
<chr>
1 apple
2 banana
whereas filter by vector c("a", "e")
, leads to the following output. Why is apple
filtered twice here?
fruits %>%
filter(str_detect(fruit, c("a", "e")))
# A tibble: 4 x 1
fruit
<chr>
1 apple
2 apple
3 banana
4 orange