1

Here's my case. I have a df with text rows and a df with IDs. I want to know if the IDs appear in my text. Here's my code – do I overlook s.th.?

df <- data.frame(text=c("random text with ID _OMaUIC4Om", 
                        "random text with ID  _tqlb6VsVN",
                        "random text with ID  0AcMp9JJ8q",
                        "random text with ID  _tqlb6VsVN",
                        "random text with ID  _tqlb6VsVN",
                        "random text with ID"))


df.id <- data.frame(id=c("_4pGCmTAat","_tqlb6VsVN","_OMaUIC4Om","0AcMp9JJ8q"))


check <- subset(df, text %in% df.id$id)
check <- subset(df, grep(df.id$id, text))
co7000
  • 221
  • 1
  • 3
  • 12

1 Answers1

1

We can paste them and pass into grepl

subset(df, grepl(paste(df.id$id, collapse="|"), text))

-output

#                              text
#1  random text with ID _OMaUIC4Om
#2 random text with ID  _tqlb6VsVN
#3 random text with ID  0AcMp9JJ8q
#4 random text with ID  _tqlb6VsVN
#5 random text with ID  _tqlb6VsVN
akrun
  • 874,273
  • 37
  • 540
  • 662