在list2中检测list1中是否包含任何字符串(整个字符串或较大字符串的一部分) [英] Detect in list2 if there are any strings (whole or part of bigger string) that is contained in list1
本文介绍了在list2中检测list1中是否包含任何字符串(整个字符串或较大字符串的一部分)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个列表:
list1<-list("q","w","e","r","t")
list2<-list("a","a","aq","c","f","g")
我需要一个可以给出TRUE
的代码,因为q
在list2
的第三个单元格中.我需要搜索list2
中list1
的每个单元格.我的意思是我需要在list2
的每个单元格中搜索list1
的每个单元格中包含的任何字符串.匹配应该是整体匹配,也应该是部分匹配(如果list1
中的字符串是list2
中较大字符串的一部分),在两种情况下,我都需要接收TRUE
.
I need a code that will give TRUE
because q
is in the third cell of list2
. I need to search for every cell of list1
in list2
. I mean that I need to search every cell of list2
for any strings that are contained in every cell of list1
. Matching should be as for the whole match but also for partial (if string from list1
is a part of the bigger string in list2
) and in both cases I need to receive TRUE
.
推荐答案
any(sapply(list1, grepl, list2))
# [1] TRUE
或等效地
greplv <- Vectorize(grepl, 'pattern')
any(greplv(list1, list2))
# [1] TRUE
这篇关于在list2中检测list1中是否包含任何字符串(整个字符串或较大字符串的一部分)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文