Marina W.
Marina W.

Reputation: 111

Keep only elements of a list which contain a string in another vector (R)

I have a vector of string keywords, and a list containing many elements with strings. I would like to keep the elements of the list that contain at least one string from the vector.

I have tried filtering with dplyr, %in%, etc.

Here is an example:

words <- c("find", "these", "words")

paragraph <- list(text1 = c("these", "words", "are", "helpful"),
              text2 = c("nothing", "to", "see", "here"),
              text3 = c("we", "can", "find", "one", "here"))

I would like to end up with a list including just text1 and text3.

Thank you!

Upvotes: 1

Views: 6347

Answers (1)

akrun
akrun

Reputation: 887511

One option is Filter from base R. Create a logical vector with %in% an wrap with any

Filter(function(x) any(words %in% x), paragraph)
#$text1
#[1] "these"   "words"   "are"     "helpful"

#$text3
#[1] "we"   "can"  "find" "one"  "here"

Or using sapply

paragraph[sapply(paragraph, function(x) any(words %in% x))]

Or using lengths and intersect

paragraph[lengths(Map(intersect, list(words), paragraph)) > 0]

or with keep from purrr

library(purrr)
keep(paragraph, ~ any(words %in% .x))

Upvotes: 5

Related Questions