Ahir Bhairav Orai
Ahir Bhairav Orai

Reputation: 697

Search if a string has two specific words

How do I search if a column of strings has these two words "medication" and "infant" anywhere in the sentence ?

For example if the column contains strings such as

  ID       Col1
  1        Quick Brown fox medication
  2        Brown fox infant
  3        Quick medication fox infant

The expected results should be just row with ID 3

 ID       Col1
  3        Quick medication fox infant

I have tried str_detect and that did not work, so any suggestions is much appreciated.

Upvotes: 1

Views: 50

Answers (3)

Neeraj
Neeraj

Reputation: 1236

Base R Approach


df[with(df, grepl("infant", Col1) & grepl("medication", Col1)),]

It is simple and easy to follow.

df <-  data.frame(id=c(1,2,3), Col1=c('Quick Brown fox medication',
                                      'Brown fox infant',
                                      'Quick medication fox infant'))

Upvotes: 3

Armel Soubeiga
Armel Soubeiga

Reputation: 69

grepl and filter can help:

df <-  data.frame(id=c(1,2,3), Col1=c('Quick Brown fox medication',
                                      'Brown fox infant',
                                      'Quick medication fox infant'))
dplyr::filter(df,grepl("medication",Col1) &
                 grepl("infant",Col1))

Output

  id                        Col1
1  3 Quick medication fox infant

Upvotes: 1

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521339

You could use grepl with two positive lookaheads:

regex <- "(?=.*\\bmedication\\b)(?=.*\\binfant\\b).*"
df[grepl(regex, df$Col1, perl=TRUE), ]

  ID                        Col1
3  3 Quick medication fox infant

Data:

df <- data.frame(
    ID=c(1,2,3),
    Col1=c("Quick Brown fox medication", "Brown fox infant",
           "Quick medication fox infant")
)

Upvotes: 4

Related Questions