Hi, Given a vector, say x=sample(0:9,10) x [1] 0 6 3 5 1 9 7 4 8 2 I can find the location of an element by which(x==2) [1] 10 but what if I want to find the location of more than one number? I could do c(which(x==2),which(x==3)) but isn't there something more streamlined? My first guess was y=c(2,3) which(x==y) integer(0) which doesn't work. I haven't found any clue in the R manual. Thanks! -- Michael Knudsen micknudsen at gmail.com http://lifeofknudsen.blogspot.com/
On 7/22/2009 3:32 PM, Michael Knudsen wrote:> Hi, > > Given a vector, say > > x=sample(0:9,10) > x > [1] 0 6 3 5 1 9 7 4 8 2 > > I can find the location of an element by > > which(x==2) > [1] 10 > > but what if I want to find the location of more than one number? I could do > > c(which(x==2),which(x==3)) > > but isn't there something more streamlined? My first guess was > > y=c(2,3) > which(x==y) > integer(0) > > which doesn't work. I haven't found any clue in the R manual.How about this? which(x %in% c(2,3))> Thanks!-- Chuck Cleland, Ph.D. NDRI, Inc. (www.ndri.org) 71 West 23rd Street, 8th floor New York, NY 10010 tel: (212) 845-4495 (Tu, Th) tel: (732) 512-0171 (M, W, F) fax: (917) 438-0894
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 HTH, Jorge On Wed, Jul 22, 2009 at 3:32 PM, Michael Knudsen <> wrote:> Hi, > > Given a vector, say > > x=sample(0:9,10) > x > [1] 0 6 3 5 1 9 7 4 8 2 > > I can find the location of an element by > > which(x==2) > [1] 10 > > but what if I want to find the location of more than one number? I could do > > c(which(x==2),which(x==3)) > > but isn't there something more streamlined? My first guess was > > y=c(2,3) > which(x==y) > integer(0) > > which doesn't work. I haven't found any clue in the R manual. > > Thanks! > > -- > Michael Knudsen > micknudsen@gmail.com > http://lifeofknudsen.blogspot.com/ > > ______________________________________________ > R-help@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. >[[alternative HTML version deleted]]
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 3In 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
On Wed, Jul 22, 2009 at 9:37 PM, Chuck Cleland<ccleland at optonline.net> wrote:> ?How about this? > > which(x %in% c(2,3))Thanks to you all! I had never thought about using %% in this context. -- Michael Knudsen micknudsen at gmail.com http://lifeofknudsen.blogspot.com/
This should work which((x==2)|(x==3)) --Quotable Quotes----------------------------- A Smile costs Nothing But Rewards Everything - Anonymous Happiness is not perfected until it is shared -Jane Porter --- On Wed, 7/22/09, Michael Knudsen <micknudsen@gmail.com> wrote: From: Michael Knudsen <micknudsen@gmail.com> Subject: [R] Find multiple elements in a vector To: "r help" <r-help@r-project.org> Date: Wednesday, July 22, 2009, 2:32 PM Hi, Given a vector, say x=sample(0:9,10) x [1] 0 6 3 5 1 9 7 4 8 2 I can find the location of an element by which(x==2) [1] 10 but what if I want to find the location of more than one number? I could do c(which(x==2),which(x==3)) but isn't there something more streamlined? My first guess was y=c(2,3) which(x==y) integer(0) which doesn't work. I haven't found any clue in the R manual. Thanks! -- Michael Knudsen micknudsen@gmail.com http://lifeofknudsen.blogspot.com/ ______________________________________________ R-help@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. [[alternative HTML version deleted]]