[R] case conversion and/or string comparison

Kaspar Pflugshaupt
Fri, 16 Nov 2001 12:45:00 -0000

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