[R] an operator for "contains"

Sarah Goslee sarah.goslee at gmail.com
Fri Jun 17 22:45:18 CEST 2005


On 6/17/05, Mike R <mike.rstat at gmail.com> wrote:
> k = c(1:9)
> if( length( which(k==3) ) ){ print("contained") }else{ print("not contained") }
> 
> is therre a simple way to test if a vector/list contains a particular value?

Yes, several. Here's one:
k <- 1:9
if(any(k == 3)) {
cat("is an element\n") } else {
cat("not an element\n") }

I don't recommend that for floating point numbers, though.

> more generally, is the a documentaion page that lists/describes all
> such operators?

help("==") would get you the help page for the actual binary
operators. Since any() isn't an operator, that won't help.

> lastly, if you didn't know the answer to my question, how would you have
> gone about searching for an answer? 

There are a variety of ways to find answers in R, although if you
aren't certain what your keyword should be, it may take a few tries.

help(), help.search(), and apropos() may all be useful.

Sarah

-- 
Sarah Goslee
http://www.stringpage.com




More information about the R-help mailing list