[R] argh .. if/else .. why?

Gabor Grothendieck ggrothendieck at gmail.com
Mon Feb 15 17:37:32 CET 2010


On Mon, Feb 15, 2010 at 11:24 AM, David Winsemius
<dwinsemius at comcast.net> wrote:
>
> On Feb 15, 2010, at 11:01 AM, hadley wickham wrote:
>
>>> I, personally, utilize the ifelse(test,statement,statement) function when
>>> possible over the methodology outlined.
>>
>> if + else and ifelse perform quite different tasks, and in general can
>> not (and should not) be exchanged.  In particular, note that for
>> ifelse, "the class attribute of the result is taken from ‘test’ and
>> may be inappropriate for the values selected from ‘yes’ and  'no’".
>
> I have always been puzzled by that bit of advice/knowledge on the help page.
> "test" will of necessity be of class "logical", and yet I regularly succeed
> in producing numeric and character vectors with ifelse. In fact ifelse would
> be rather limited in utility if it only returned logical vectors.
>

I think it had intended to refer to oldClass rather than class.

> oldClass(TRUE)
NULL
> oldClass(ifelse(TRUE, 1, 2))
NULL



More information about the R-help mailing list