Hi Consider the following code> x <- rep(1:13, 13)> y <- 1:3I 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? Thanks Rainer -- Rainer M. Krug, Dipl. Phys. (Germany), MSc Conservation Biology (UCT) Plant Conservation Unit Department of Botany University of Cape Town Rondebosch 7701 South Africa
Use %in%: x [ x %in% y ] G. On Fri, Mar 14, 2008 at 12:37:45PM +0200, Rainer M Krug wrote:> Hi > > 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? > > Thanks > > Rainer > -- > 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 > 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.-- Csardi Gabor <csardi at rmki.kfki.hu> UNIL DGM
Try this: x[x %in% y] On 14/03/2008, Rainer M Krug <r.m.krug at gmail.com> wrote:> Hi > > 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? > > Thanks > > Rainer > -- > 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 > 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. >-- Henrique Dallazuanna Curitiba-Paran?-Brasil 25? 25' 40" S 49? 16' 22" O