Search code examples
rdataframetibble

How to check if any character in the character vector is inside any character in another list


I have a rows of vectors in a single in the tibble dataframe and I have a set of filtered words. I want to filter the rows if the characters inside the vectors did not appear inside the filtered set. Eg. Based on the given table, only row 1, 3 and 4 should be retained. I tried using the filter function but because vectors return True True, it seems to be filtered away and does not remain inside the data table. Is there any function I could use to check for words inside a list based on another list?

tags
c("hello", "hi")
c("blanks", "nothing")
c("thanks", "welcome")
hi
filtered_words <- c("hi","bye","thanks")

Solution

  • With any:

    dataset[sapply(dataset$tags, \(x) any(x %in% filtered_words)), ]