[R] compare multiple values with vector and return vector

Rolf Turner r.turner at auckland.ac.nz
Sat Apr 3 00:42:21 CEST 2010


On 3/04/2010, at 11:35 AM, Joris Meys wrote:

> Dear all,
> 
> I have a vector, and for each element I want to check whether it is equal to
> any element from another vector. I want a vector of logical values with the
> length of the first one as return. In R this would be :
> 
>> x <- 1:10
>> sapply(x,function(y){any(y==c("2","3","4"))})
> [1] FALSE  TRUE  TRUE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE
> 
> It works pretty smooth, but I have the feeling there's a less complicated
> way of doing it. My code should be readable by programmers who are not
> really familiar with R, but I hate to use for-loops as I have pretty huge
> datasets. Anybody an idea?
> thank you in advance.

?"%in%"

	cheers,

		Rolf Turner
######################################################################
Attention: 
This e-mail message is privileged and confidential. If you are not the 
intended recipient please delete the message and notify the sender. 
Any views or opinions presented are solely those of the author.

This e-mail has been scanned and cleared by MailMarshal 
www.marshalsoftware.com
######################################################################



More information about the R-help mailing list