How to compare character vectors for partial matches in R

This should be a simple question, but as a newbie to R, I couldn't figure it out.

I have two character vectors List1 and List2, and I would like to know how many examples in List1 are also found in List2. But List2 often contains several names that seem to confuse things. Here are the hypothetical lists:

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

I can get output that identifies SampleX and SampleY, but not SampleZ.

Any suggestions

Thanks!!

+6
source share
1 answer

What about:

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

?

+8
source

Source: https://habr.com/ru/post/916015/


All Articles