Personally, I like using grepl()
for those kind of problems. You can play around the regex to select rows. (See example here)
df <- data.frame(list("fruits" = c("Apple", "Apple, Orange", "Kiwi, Apple", "Kiwi")))
Visualization of df:
| id | fruits |
|----|---------------|
| 1 | Apple |
| 2 | Apple, Orange |
| 3 | Kiwi, Apple |
| 3 | Kiwi |
Then you can write:
df_only_apples <- df[grepl("[Aa]pple", df$fruits),, drop=FALSE]
which will give you
| id | fruits |
|----|---------------|
| 1 | Apple |
| 2 | Apple, Orange |
| 3 | Kiwi, Apple |
But if you want to select rows that contain "Apples" and "Oranges" you could just write df[grepl("([Aa]pple|[Oo]range)", df$fruits)