[R] matching elements from two vectors

jim holtman jholtman at gmail.com
Fri Aug 17 16:54:24 CEST 2007


Also if you want all the matches

> x[x %in% y]
[1] 2 3 3 3


On 8/17/07, Gonçalo Ferraz <gferraz29 at gmail.com> wrote:
> Hi,
>
> Imagine a vector x with elements (1,2,1,1,3,5,3,3,1) and a vector y with
> elements (2,3). I need to find out what elements of x match any of the
> elements of y.
>
> Is there a simple command that will return a vector with elements
> (F,T,F,F,T,F,T,T,F). Ideally, I would like a solution that works with
> dataframe colums as well.
>
> I have tried x==y and it doesn't work.
> x==any(y) doesn't work either. I realize I could write a foor loop and go
> through each element of y asking if it matches any element of x, but isn't
> there a shorter way?
>
> Thanks,
> Gonçalo
>
>        [[alternative HTML version deleted]]
>
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
>


-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem you are trying to solve?



More information about the R-help mailing list