[R] Find value in vector (or matrix)

Prof Brian Ripley ripley at stats.ox.ac.uk
Thu Nov 20 13:41:15 CET 2003


On Thu, 20 Nov 2003, Pascal A. Niklaus wrote:

> 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" %in" y,

which disguises a call to match(), so look at match.

length(grep("a", y)) > 0 should work too.

>     {
>           # "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

LETTERS[1:10] will do this.

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595




More information about the R-help mailing list