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]]
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]]
> x <- c(1,2,1,1,3,5,3,3,1) > y <- c(2,3) > intersect(x,y)[1] 2 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?
Hi! v1 <- c(1,2,1,1,3,5,3,3,1) v2 <- c(2,3) v1 %in% v2 -- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O On 17/08/07, Gonçalo Ferraz <gferraz29@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@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. > >[[alternative HTML version deleted]]
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?