Find matches of a vector of strings in another vector of strings

前端 未结 1 1032
被撕碎了的回忆
被撕碎了的回忆 2020-12-06 05:47

I\'m trying to create a subset of a data frame of news articles that mention at least one element of a set of keywords or phrases.

# Sample data frame of ar         


        
相关标签:
1条回答
  • 2020-12-06 06:06

    You can try pasting your "keywords" together and separate them with the pipe character (|) which will work like an "or", like this:

    > articles[grepl(paste(keywords, collapse="|"), articles$text),]
      id                                                                         text
    1  1     Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
    2  2 tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
    4  4    consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
    
    0 讨论(0)
提交回复
热议问题