[R] matching elements from two vectors

davidr at rhotrading.com davidr at rhotrading.com
Fri Aug 17 16:51:23 CEST 2007


x %in% y

David L. Reiner
Rho Trading Securities, LLC


-----Original Message-----
From: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Gonçalo Ferraz
Sent: Friday, August 17, 2007 9:46 AM
To: r-help at stat.math.ethz.ch
Subject: [R] matching elements from two vectors

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



More information about the R-help mailing list