[R] Find multiple elements in a vector

Keith Jewell k.jewell at campden.co.uk
Thu Jul 23 17:05:07 CEST 2009


Try
which(x %in% y)

HTH

KJ

"Philip Twumasi-Ankrah" <nana_kwadwo_derkyi at yahoo.com> wrote in message 
news:519822.56196.qm at web39502.mail.mud.yahoo.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 at gmail.com> wrote:

From: Michael Knudsen <micknudsen at gmail.com>
Subject: [R] Find multiple elements in a vector
To: "r help" <r-help at 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 at gmail.com
http://lifeofknudsen.blogspot.com/

______________________________________________
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.




[[alternative HTML version deleted]]




--------------------------------------------------------------------------------


>




More information about the R-help mailing list