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
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 anymoreLETTERS[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
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 > > PascalYou 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
On Thu, Nov 20, 2003 at 01:26:23PM +0100, Pascal A. Niklaus wrote:> Is there a function to check if a particular value is contained in a > vector?> myvec <- c('foo','bar','duh','bonk') > 'bonk' %in% myvec[1] TRUE> 'asdf' %in% myvec[1] FALSE> Also, is there a way to generate character sequences similar to numeric > sequences, e.g. something like "A":"J" ?> LETTERS[1:5][1] "A" "B" "C" "D" "E"> letters[c(8,9,12,23)][1] "h" "i" "l" "w" cu Philipp -- Dr. Philipp Pagel Tel. +49-89-3187-3675 Institute for Bioinformatics / MIPS Fax. +49-89-3187-3585 GSF - National Research Center for Environment and Health Ingolstaedter Landstrasse 1 85764 Neuherberg, Germany
Pascal A. Niklaus wrote:> y <- c("a","b","c") > if("a" in y) > { > # "a" is not in y > }You are 98% of the way there. The missing two percent is this: if("a" %in% y) { ... } you could also use the 'any' function: if(any(y=="a")){ ... }> 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 anymoreR defines two vectors, "letters" and "LETTERS" which have the 26 lower and upper-case letters of the English alphabet. Subset from them to get what you want. Baz
"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 anymoreLETTERS[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