Home > Blockchain >  Filter all sentences containing this and that word, R
Filter all sentences containing this and that word, R

Time:06-10

I have a dataset with a text column of millions of sentences. I want to filter out all the sentences that contain either of these words

immigr* 
migra*
asyl*
refug*

Is there a neat little function for that which I don't know of? So far I've tried with

grep('immigr ', df$text)

But it can only take one word at the time...

CodePudding user response:

You can collapse values with or (|):

wd <- c("immigr*","migra*", "asyl*", "refug*")
grep(paste(wd, collapse = "|"), c("immigra", "other"))
1
  • Related