[R] searching a vector

foobar wwsprague at ucdavis.edu
Fri Apr 30 23:51:51 CEST 2004

Arend P. van der Veen wrote:

> Hi,
> I have a integer vector x that contains a unique set of numbers: 
> x <- c(1,2,4,6,8,10,12)
> Is there a simple test I can use to determine if an integer such as 6 is
> contained in x ?  

Try the following and experiment:

x %in% c(2,4)
which(x %in% c(2,4))
x[which(x %in% c(2,4)) ]

1.  logical vector w/ TRUE's where one of the c(2,4) are located, F's 
2.  indexes of the TRUE's generated above
3.  the vector subset from those indexes

Sorry to be so terse, but that might help
> Thanks in advance for any help,
> Arend
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html

More information about the R-help mailing list