This is no doubt trivial but after searching the help files and the web, I cannot seem to find it. 1) How do I convert 'hgt' into 'HGT' in R? 2) How should I have used the help facilities to find this? At the end of the day, all I want to do is case insensitive string matching... i.e. 'if ("HGT" == 'hgt') print('this should be true')' I tried using some of the regular expression tools but I had some trouble interpreting the results: > grep("HGT", "hgt", ignore.case = T) [1] 1 > grep("HGT", "tem", ignore.case = T) numeric(0) This appears to be what I want. However, putting an 'if' around this fails for the false case. > if ( grep("HGT", "tem", ignore.case = T) ) print('hi'); Error in if (grep("HGT", "tem", ignore.case = T)) print("hi") : argument of if(*) is not interpretable as logical I suppose that R cannot interpret 'if (numeric(0))'. I would think the following would work? > grep("HGT", "hgt", ignore.case = T) == 1 [1] TRUE > grep("HGT", "tem", ignore.case = T) == 1 logical(0) Note that the bottom is not 'FALSE'? Why? > if (grep("HGT", "tem", ignore.case = T) == 1) print('hi'); Error in if (grep("HGT", "tem", ignore.case = T) == 1) print("hi") : missing value where logical needed I suppose that R cannot interpret 'if (logical(0))' either? So my question is: How to I return a logical 'TRUE/FALSE' when comparing 2 strings? Clearly, I am missing something. Thanks in advance, Randall -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
> > > grep("HGT", "hgt", ignore.case = T) == 1 > [1] TRUE > > grep("HGT", "tem", ignore.case = T) == 1 > logical(0) > > Note that the bottom is not 'FALSE'? Why? >That's because grep returns the *indices* of the elements of x that yielded a match. If there are no matches, then grep returns a (numeric) vector of length 0. A quick and dirty way of achieving what you want would therefore be to test the length of the vector returned, eg, if (length(grep("HGT","tem",ignore.case=T))==0) print("hi") I'm sure there is a better way, though! Dave -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Here are some hacks: test <- "BIG" letters[match(strsplit(test,"")[[1]],LETTERS)] test2 <- "BIGsmall" v <- strsplit(test2,"")[[1]] ucase <- v %in% LETTERS v[ucase] <- letters[match(v[ucase],LETTERS)] paste(v,collapse="") I'm not sure how you would have found this in the help system ... On Fri, 16 Nov 2001, Randall Skelton wrote:> This is no doubt trivial but after searching the help files and the web, I > cannot seem to find it. > > 1) How do I convert 'hgt' into 'HGT' in R? > 2) How should I have used the help facilities to find this? > > At the end of the day, all I want to do is case insensitive string > matching... i.e. 'if ("HGT" == 'hgt') print('this should be true')' > > I tried using some of the regular expression tools but I had some trouble > interpreting the results: > > > grep("HGT", "hgt", ignore.case = T) > [1] 1 > > grep("HGT", "tem", ignore.case = T) > numeric(0) > > This appears to be what I want. However, putting an 'if' around this > fails for the false case. > > > if ( grep("HGT", "tem", ignore.case = T) ) print('hi'); > Error in if (grep("HGT", "tem", ignore.case = T)) print("hi") : > argument of if(*) is not interpretable as logical > > I suppose that R cannot interpret 'if (numeric(0))'. I would think the > following would work? > > > grep("HGT", "hgt", ignore.case = T) == 1 > [1] TRUE > > grep("HGT", "tem", ignore.case = T) == 1 > logical(0) > > Note that the bottom is not 'FALSE'? Why? > > > if (grep("HGT", "tem", ignore.case = T) == 1) print('hi'); > Error in if (grep("HGT", "tem", ignore.case = T) == 1) print("hi") : > missing value where logical needed > > I suppose that R cannot interpret 'if (logical(0))' either? > > So my question is: How to I return a logical 'TRUE/FALSE' when > comparing 2 strings? Clearly, I am missing something. > > Thanks in advance, > Randall > > -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- > r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html > Send "info", "help", or "[un]subscribe" > (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch > _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._ >-- 318 Carr Hall bolker at zoo.ufl.edu Zoology Department, University of Florida http://www.zoo.ufl.edu/bolker Box 118525 (ph) 352-392-5697 Gainesville, FL 32611-8525 (fax) 352-392-3704 -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
On 16.11.2001 12:37 Uhr, Randall Skelton wrote:> This is no doubt trivial but after searching the help files and the web, I > cannot seem to find it. > > 1) How do I convert 'hgt' into 'HGT' in R?There are the functions tolower() and toupper(). They work as one would expect:> toupper("hgt")[1] "HGT" With either of them, the comparison you mention is easy: compareCaseless <- function(string1, string2) return(toupper(string1) == toupper(string2))> compareCaseless("hgt", "HGT")[1] TRUE> 2) How should I have used the help facilities to find this? >Hmm. help.search("case") does not give them, nor does help.search("string"). Nor do, in fact, help.search("upper") or "lower". I just found them indirectly via help.search("character"), which mentioned the chartr() function. On its help page, those two functions are noted. Maybe they deserve to be better advertised? Cheers Kapar Pflugshaupt -- Kaspar Pflugshaupt Geobotanisches Institut Zuerichbergstr. 38 CH-8044 Zuerich Tel. ++41 1 632 43 19 Fax ++41 1 632 12 15 mailto:pflugshaupt at geobot.umnw.ethz.ch privat:pflugshaupt at mails.ch http://www.geobot.umnw.ethz.ch -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
On Fri, 16 Nov 2001, Randall Skelton wrote:> This is no doubt trivial but after searching the help files and the web, I > cannot seem to find it. > > 1) How do I convert 'hgt' into 'HGT' in R?Use toupper().> 2) How should I have used the help facilities to find this?We need real keywords in the help system. One day ....> At the end of the day, all I want to do is case insensitive string > matching... i.e. 'if ("HGT" == 'hgt') print('this should be true')'As in fitdistr (in MASS) which has distname <- tolower(densfun) densfun <- switch(distname, beta = dbeta, cauchy = dcauchy, "chi-squared" = dchisq, exponential = dexp, f = df, gamma = dgamma, "log-normal" = dlnorm, lognormal = dlnorm, logistic = dlogis, "negative binomial" = dnbinom, normal = dnorm, t = mydt, uniform = dunif, weibull = dweibull, NULL) ....> I tried using some of the regular expression tools but I had some trouble > interpreting the results: > > > grep("HGT", "hgt", ignore.case = T) > [1] 1 > > grep("HGT", "tem", ignore.case = T) > numeric(0) > > This appears to be what I want. However, putting an 'if' around this > fails for the false case. > > > if ( grep("HGT", "tem", ignore.case = T) ) print('hi'); > Error in if (grep("HGT", "tem", ignore.case = T)) print("hi") : > argument of if(*) is not interpretable as logical > > I suppose that R cannot interpret 'if (numeric(0))'. I would think the > following would work? > > > grep("HGT", "hgt", ignore.case = T) == 1 > [1] TRUE > > grep("HGT", "tem", ignore.case = T) == 1 > logical(0) > > Note that the bottom is not 'FALSE'? Why? > > > if (grep("HGT", "tem", ignore.case = T) == 1) print('hi'); > Error in if (grep("HGT", "tem", ignore.case = T) == 1) print("hi") : > missing value where logical needed > > I suppose that R cannot interpret 'if (logical(0))' either?Right. You would do better to use regexpr than grep.> > So my question is: How to I return a logical 'TRUE/FALSE' when > comparing 2 strings? Clearly, I am missing something. > > Thanks in advance, > Randall > > -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- > r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html > Send "info", "help", or "[un]subscribe" > (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch > _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._ >-- 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 272860 (secr) Oxford OX1 3TG, UK Fax: +44 1865 272595 -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
?toupper converts lower-case to upper-case. Jan ------------------------------------------------- designed for _monospaced_ font ------------------------------------------------- /- Jan Svatos, PhD Sokolovska 855/225 -/ /- Data Analyst, Prague 9 -/ /- Eurotel Praha 190 00 -/ /- jan_svatos at eurotel.cz Czechia -/ ------------------------------------------------- - - - Original message: - - - From: owner-r-help at stat.math.ethz.ch Send: 11/16/01 2:53:33 PM To: <r-help at stat.math.ethz.ch> Subject: [R] case conversion and/or string comparison This is no doubt trivial but after searching the help files and the web, I cannot seem to find it. 1) How do I convert 'hgt' into 'HGT' in R? 2) How should I have used the help facilities to find this? At the end of the day, all I want to do is case insensitive string matching... i.e. 'if ("HGT" == 'hgt') print('this should be true')' I tried using some of the regular expression tools but I had some trouble interpreting the results: > grep("HGT", "hgt", ignore.case = T) [1] 1 > grep("HGT", "tem", ignore.case = T) numeric(0) This appears to be what I want. However, putting an 'if' around this fails for the false case. > if ( grep("HGT", "tem", ignore.case = T) ) print('hi'); Error in if (grep("HGT", "tem", ignore.case = T)) print("hi") : argument of if(*) is not interpretable as logical I suppose that R cannot interpret 'if (numeric(0))'. I would think the following would work? > grep("HGT", "hgt", ignore.case = T) == 1 [1] TRUE > grep("HGT", "tem", ignore.case = T) == 1 logical(0) Note that the bottom is not 'FALSE'? Why? > if (grep("HGT", "tem", ignore.case = T) == 1) print('hi'); Error in if (grep("HGT", "tem", ignore.case = T) == 1) print("hi") : missing value where logical needed I suppose that R cannot interpret 'if (logical(0))' either? So my question is: How to I return a logical 'TRUE/FALSE' when comparing 2 strings? Clearly, I am missing something. Thanks in advance, Randall -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. -.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._. _._._ -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
On Fri, 16 Nov 2001, Randall Skelton wrote: <stuff that was already answered>> > I suppose that R cannot interpret 'if (numeric(0))'. I would think the > following would work? > > > grep("HGT", "hgt", ignore.case = T) == 1 > [1] TRUE > > grep("HGT", "tem", ignore.case = T) == 1 > logical(0) > > Note that the bottom is not 'FALSE'? Why?In S everything is a vector, so TRUE is actually a vector of 1 logical value and logical(0) is a vector of no logical values. For many functions the sensible thing to do is to operate on the vector one element at a time, but if() can't do that -- it has to take one branch or the other. So for if() you need a vector of length 1 -- it has to make 1 decision. If you give it more than one logical value it uses the first one to make its decision; if you give it fewer than one it doesn't know what to do. One other way around this is to use any(). any() is TRUE if its argument is TRUE (or a number other than 0) and FALSE if its argument is FALSE, 0, or has zero length. -thomas -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._