Search code examples
rregexstringcompare

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 ;)


Solution

  • 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