[R] case conversion and/or string comparison
Thomas Lumley
tlumley at u.washington.edu
Sat Nov 17 19:54:17 CET 2001
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
More information about the R-help
mailing list