简体   繁体   中英

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.

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. But List2 often has multiple names put together which seems to be messing things up. 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.

Any suggestions??

Thanks!!

How about:

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

?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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