[R] Selecting elements in vector

From: Rainer M Krug <r.m.krug_at_gmail.com>
Date: Fri, 14 Mar 2008 12:37:45 +0200


Consider the following code

> x <- rep(1:13, 13)

> y <- 1:3

I want to select all elements in x which are equal to 1, 2 or 3.

I know that I could use

> sel <- x==y[1] | x==y[2] | x==y[3]
> x[sel]

to obtain the values, but in my analysis, the y-vector is thousands of elements long.

Is there any way, that I can do that easily?



Rainer M. Krug, Dipl. Phys. (Germany), MSc Conservation Biology (UCT)

Plant Conservation Unit Department of Botany
University of Cape Town
Rondebosch 7701
South Africa

R-help_at_r-project.org mailing list
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Received on Fri 14 Mar 2008 - 11:00:16 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Fri 14 Mar 2008 - 12:30:22 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive