DT_result <- data.table(lst_B = c("RNR_B","BC_TET")) I want: > DT_result <- data.table(lst_B = c("RNR_B","BC_TET")" /> DT_result <- data.table(lst_B = c("RNR_B","BC_TET")) I want: > DT_result <- data.table(lst_B = c("RNR_B","BC_TET")" /> DT_result <- data.table(lst_B = c("RNR_B","BC_TET")) I want: > DT_result <- data.table(lst_B = c("RNR_B","BC_TET")"/>

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

26.2k views Asked by At

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.

4

There are 4 answers

0
G5W On BEST ANSWER

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
0
Onyambu On
    DT_result[,results:=sapply(lst_A,function(x)any(grepl(x,lst_B)))][]
    lst_B results
1:  RNR_B    TRUE
2: BC_TET    TRUE
0
thelatemail On

Loop over each option with grepl and then combine with an | ('OR'):

DT_result[, hit := Reduce(`|`, Map(grepl, lst_A, .(lst_B)))]
DT_result

#    lst_B  hit
#1:  RNR_B TRUE
#2: BC_TET TRUE
0
BENY On

Using stringr's str_detect

stringr::str_detect(DT_result$lst_B,'TET|RNR')
[1] TRUE TRUE

#DF['hit']=stringr::str_detect(DT_result$lst_B,'TET|RNR')