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

LeGeniusII picture LeGeniusII · Jun 4, 2018 · Viewed 15.7k times · Source

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.

Answer

G5W picture G5W · Jun 4, 2018

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