简体   繁体   English

如何比较R中部分匹配的字符向量

[英]How to compare characters vectors for partial matches in R

This must be a simple question, but as an R newcomer I haven't been able to figure it out. 这必须是一个简单的问题,但作为一个R新人,我无法弄明白。

I have two character vectors, List1 and List2, and I would like to know how many of the samples in List1 are also found in List2. 我有两个字符向量,List1和List2,我想知道在List2中也找到了List1中有多少样本。 But List2 often has multiple names put together which seems to be messing things up. 但是List2经常有多个名字放在一起,似乎搞砸了。 Here are the hypothetical lists: 以下是假设列表:

List1 <- c("SampleX", "SampleY", "SampleZ", "SampleQ")

List2 <- c("SampleX", "SampleY", "Alias1,Alias2,SampleZ")

I can get an output that identifies SampleX and SampleY, but not SampleZ. 我可以得到一个标识SampleX和SampleY的输出,但不能得到SampleZ。

Any suggestions?? 有什么建议??

Thanks!! 谢谢!!

How about: 怎么样:

List1[sapply(List1,function(x) any(grepl(x,List2)))]
[1] "SampleX" "SampleY" "SampleZ"

?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM