[R] matching elements from two vectors

Martin Maechler maechler at stat.math.ethz.ch
Fri Aug 17 19:12:44 CEST 2007


>>>>> "GF" == Gonçalo Ferraz <gferraz29 at gmail.com>
>>>>>     on Fri, 17 Aug 2007 11:03:09 -0400 writes:

    GF> Thanks much. x %in% y works.

yes, indeed.
But I wonder a bit why nobody remarked on the facts that

1) you use "match" in the subject of your e-mail

2) match() is the basic ingredient you use:
   '%in%' is trivially defined via match().
   Type  `%in%`  in your R session.

Regards, Martin

    GF> On 8/17/07, jim holtman <jholtman at gmail.com> wrote:
    >> 
    >> 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?
    >> 

    GF> [[alternative HTML version deleted]]

    GF> ______________________________________________
    GF> R-help at stat.math.ethz.ch mailing list
    GF> https://stat.ethz.ch/mailman/listinfo/r-help
    GF> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
    GF> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list