Search code examples
rstringdata.tablegreplcontain

R - test if a string vector contains any element of another list


I have:

> lst_A <- c("TET","RNR")
> DT_result <- data.table(lst_B = c("RNR_B","BC_TET"))

I want:

> DT_result <- data.table(lst_B = c("RNR_B","BC_TET"), result = c(TRUE,TRUE))
> DT_result
    lst_B result
1:  RNR_B   TRUE
2: BC_TET   TRUE

Basically, for each element in 'lst_B' if it contains any element in 'lst_A' then TRUE otherwise FALSE.


Solution

  • You can get this using grepl.

    lst_A <- c("TET","RNR")
    lst_B = c("RNR_B","BC_TET")
    
    Pattern = paste(lst_A, collapse="|")
    grepl(Pattern, lst_B)
    
    library(data.table)
    DT_result <- data.table(lst_B, result=grepl(Pattern, lst_B))
    DT_result
        lst_B result
    1:  RNR_B   TRUE
    2: BC_TET   TRUE
    

    Addition

    To respond to a comment, here is an example with more strings to test. Some pass the test, others not.

    lst_A <- c("TET","RNR")
    lst_B = c("RNR_B","BC_TET", "Fred", "RNR_A", "Zero", "ABC_TET")
    
    Pattern = paste(lst_A, collapse="|")
    
    DT_result <- data.table(lst_B, result=grepl(Pattern, lst_B))
    DT_result
         lst_B result
    1:   RNR_B   TRUE
    2:  BC_TET   TRUE
    3:    Fred  FALSE
    4:   RNR_A   TRUE
    5:    Zero  FALSE
    6: ABC_TET   TRUE