Re: [R] Find multiple elements in a vector

2009-07-23 Thread Keith Jewell
perfected until it is shared -Jane Porter --- On Wed, 7/22/09, Michael Knudsen wrote: From: Michael Knudsen Subject: [R] Find multiple elements in a vector To: "r help" 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 c

Re: [R] Find multiple elements in a vector

2009-07-23 Thread Philip Twumasi-Ankrah
  -Jane Porter   --- On Wed, 7/22/09, Michael Knudsen wrote: From: Michael Knudsen Subject: [R] Find multiple elements in a vector To: "r help" 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 ca

Re: [R] Find multiple elements in a vector

2009-07-22 Thread Michael Knudsen
On Wed, Jul 22, 2009 at 9:37 PM, Chuck Cleland 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 micknud...@gmail.com http://lifeofknudsen.blogspot.com/ __ R-h

Re: [R] Find multiple elements in a vector

2009-07-22 Thread Steve Lianoglou
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

Re: [R] Find multiple elements in a vector

2009-07-22 Thread Jorge Ivan Velez
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 f

Re: [R] Find multiple elements in a vector

2009-07-22 Thread Chuck Cleland
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)

[R] Find multiple elements in a vector

2009-07-22 Thread Michael Knudsen
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