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!!
We can use the match() function to match the values in two vectors. We'll be using it to evaluate which values are present in both vectors, and how to reorder the elements to make the values match.
(A partial match occurs if the whole of the element of x matches the beginning of the element of table .) Finally, all remaining elements of x are regarded as unmatched. In addition, an empty string can match nothing, not even an exact match to an empty string.
A partial match is one that matched one or more characters at the end of the text input, but did not match all of the regular expression (although it may have done so had more input been available).
How about:
List1[sapply(List1,function(x) any(grepl(x,List2)))]
[1] "SampleX" "SampleY" "SampleZ"
?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With