[R] case conversion and/or string comparison

David Barron david.barron at jesus.ox.ac.uk
Fri Nov 16 12:56:48 CET 2001


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



More information about the R-help mailing list