[prev in list] [next in list] [prev in thread] [next in thread] 

List:       r-help
Subject:    Re: [R] Find value in vector (or matrix)
From:       Sundar Dorai-Raj <sundar.dorai-raj () pdf ! com>
Date:       2003-11-20 12:44:05
[Download RAW message or body]



Pascal A. Niklaus wrote:
> Hi all,
> 
> Is there a function to check if a particular value is contained in a 
> vector? I've looked at grep in the hope that I could use a Perl-like 
> syntax, but obviously it's different...
> 
> I'd like to do something like:
> 
>    y <- c("a","b","c")
>    if("a" in y)
>    {
>          # "a" is not in y
>    }
> 
> Also, is there a way to generate character sequences similar to numeric 
> sequences, e.g. something like "A":"J" ? I remember having seen a 
> command doing this somewhere, but can't find it anymore
> 
> Thanks
> 
> Pascal

You almost have it: Try "%in%" instead.

if("a" %in% y) { }

or

if(any(c("a", "b") %in% y)) { }

See help("%in%").

For question 2, see ?letters or ?LETTERS.

-sundar


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic