[R] Find multiple elements in a vector

Michael Knudsen micknudsen at gmail.com
Wed Jul 22 21:32:26 CEST 2009


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/




More information about the R-help mailing list