Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check whether a string appears in another in R

I've got a tibble containing sentences like that :

df <- tibble(sentences = c("Bob is looking for something", "Adriana has an umbrella", "Michael is looking at..."))

And another containing a long list of names :

names <- tibble(names = c("Bob", "Mary", "Michael", "John", "Etc."))

I would like to see if the sentences contain a name from the list and add a column to indicate if this is the case and get the following tibble :

wanted_df <- tibble(sentences = c("Bob is looking for something", "Adriana has an umbrella", "Michael is looking at..."), check = c(TRUE, FALSE, TRUE))

So far I've tried that, with no success :

df <- df %>%
mutate(check = grepl(pattern = names$names, x = df$sentences, fixed = TRUE))

And also :

check <- str_detect(names$names %in% df$sentences)

Thanks a lot for any help ;)

like image 255
Malichot Avatar asked Oct 27 '25 05:10

Malichot


1 Answers

You should form a single regex expression in grepl:

df %>% 
  mutate(check = grepl(paste(names$names, collapse = "|"), sentences))

# A tibble: 3 × 2
  sentences                    check
  <chr>                        <lgl>
1 Bob is looking for something TRUE 
2 Adriana has an umbrella      FALSE
3 Michael is looking at...     TRUE 
like image 161
Maël Avatar answered Oct 28 '25 18:10

Maël



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!