[R] case conversion and/or string comparison

Prof Brian Ripley ripley at stats.ox.ac.uk
Fri Nov 16 13:48:59 CET 2001


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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list