[R] case conversion and/or string comparison

Thomas Lumley
Sat, 17 Nov 2001 18:54:17 -0000

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