[R] else if statement error

Uwe Ligges ligges at statistik.tu-dortmund.de
Tue Oct 6 16:15:07 CEST 2009



Gabor Grothendieck wrote:
> 2009/10/6 Uwe Ligges <ligges at statistik.tu-dortmund.de>:
>> The first rule is easy: As long as you are using scalar valued (i.e. length
>> 1 vectors in R) "cond", you should prefer
>>  if(cond) cons.expr  else  alt.expr
>> rather than
>>  ifelse(cond, yes, no)
>> because the latter one evaluates both "yes" and "no" while the former one
>> evaluates exactly one of both expressions.
> 
> I don't think that that is true.  The false leg was not evaluated here:
> 
>> ifelse(TRUE, { cat("a"); 1}, {cat("b"); 2})
> a[1] 1


Ah, indeed that changed at some point and I forgot that the code checks 
for the length of cond nowadays. Thanks for pointing it out.

Best wishes,
uwe




More information about the R-help mailing list