Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compare characters vectors for partial matches in R

Tags:

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!!

like image 816
user1401333 Avatar asked May 17 '12 17:05

user1401333


People also ask

How do I match two values of a vector in R?

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.

What is partial matching in R?

(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.

What is partial match?

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).


1 Answers

How about:

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

?

like image 150
Ben Bolker Avatar answered Nov 01 '22 00:11

Ben Bolker