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

Time:11-01

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")

CodePudding user response:

With any:

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

CodePudding user response:

You can use the dplyr::filter() function if you also use rowwise():

filter(rowwise(df), any(filtered_words %in% tags))
  • Related