[R] Find value in vector (or matrix)

Peter Dalgaard p.dalgaard at biostat.ku.dk
Thu Nov 20 14:47:49 CET 2003


"Pascal A. Niklaus" <Pascal.Niklaus at unibas.ch> writes:

> 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
>     }

"a" %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] 

is probably the closest. You do need to look up that "J" is the 10th
letter or resort to things like

LETTERS[which(LETTERS>="A"&LETTERS<="J")]

or

LETTERS[which(LETTERS=="A"):which(LETTERS=="J")]


-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: (+45) 35327907




More information about the R-help mailing list