Another R inferno: if () {} else {} + x

if (TRUE) {
    0
} else {
    2
} + 3


will return 0 and not 3 because the priority order is:

if (TRUE) then {0}
else
{2} + 3


This is clearly counter-intuitive.


As a general rule, I propose to never use result of a if statement directly in a computing.


Note that ifelse() gives more normal result:
ifelse(TRUE, 0, 2) + 3
will return 3

Commentaires

Posts les plus consultés de ce blog

Standard error from Hessian Matrix... what can be done when problem occurs

stepAIC from package MASS with AICc

Install treemix in ubuntu 20.04