[R] A kind of set operation in R

Berend Hasselman bhh at xs4all.nl
Thu Apr 5 21:48:47 CEST 2012


On 05-04-2012, at 21:32, Julio Sergio wrote:

> I have an ordered "set" of numbers, represented by a vector, say
> 
>> X <- c(10:13, 17,18)
>> X
>  [1] 10 11 12 13 17 18
> 
> then I have a "sub-set" of X, say
> 
>> Y <- c(11,12,17,18)
> 
> Is there a simple way in R to have a logical vector (parallel to X) indicating 
> what elements of X are in Y, i.e.,
> 
>> Inclusion
>  [1] FALSE TRUE TRUE FALSE TRUE TRUE
> 
> I'm trying to avoid looping over both vectors to produce the tests. Do you have 
> any comments on this?

Try

X %in% Y

You could also have a look at match

Berend



More information about the R-help mailing list