[R] Find multiple elements in a vector

Steve Lianoglou mailinglist.honeypot at gmail.com
Wed Jul 22 21:58:02 CEST 2009


Hi,

On Jul 22, 2009, at 3:39 PM, Jorge Ivan Velez wrote:

> Dear Michael,
> Take a look at ?"%in%"   This is an example:
>
> set.seed(123)
> x <- sample(0:9,10)
> y <- c(2,3)
> which(x %in% y)
> # [1] 1 3

In addition to the above, you can also use the `match` function:

match(c(2,3), x)
[1] 1 3

The problem is that if you have repeat elements in x, it will only  
return you the index of the first match in x.

-steve

--
Steve Lianoglou
Graduate Student: Physiology, Biophysics and Systems Biology
Weill Medical College of Cornell University

Contact Info: http://cbio.mskcc.org/~lianos/contact




More information about the R-help mailing list