[R] Checking if an element is part of a vector

Marc Schwartz MSchwartz at MedAnalytics.com
Wed Oct 6 18:38:02 CEST 2004


On Wed, 2004-10-06 at 11:23, Dominik Bach wrote:
> Hi!
> 
> I want to know if a given element <- ('c64') is part of a vector<-
> ('c64',  'amiga', 'atari'). 
> 
> Does a function exist for this which gives back a logical value?
> 
> thankx
> db

> v <- c("c64",  "amiga", "atari", "MITS Altair", "pong")

> "c64" %in% v
[1] TRUE

Also, for the position in the vector, you can use:

> which(v == "c64")
[1] 1


See ?"%in%" and ?which for more information.

HTH,

Marc Schwartz




More information about the R-help mailing list