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
基本上,对于“lst_B”中的每个元素,如果它包含“lst_A”中的任何元素,则为 TRUE,否则为 FALSE。
你可以使用这个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
为了回复评论,这里有一个包含更多字符串进行测试的示例。有些通过了测试,有些则没有。
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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)