[R] evaluation of the alternative expression in ifelse

Renaud Lancelot renaud.lancelot at gmail.com
Sun Jun 4 19:53:54 CEST 2006


Dear all,

I am trying to avoid the warnings produced by:

> x <- -2:2
> log(x)
[1]       NaN       NaN      -Inf 0.0000000 0.6931472
Warning message:
production de NaN in: log(x)

I thought that using ifelse would be a solution, but it is not the case:

> ifelse(test = x < 0, yes = NaN, no = log(x))
[1]       NaN       NaN      -Inf 0.0000000 0.6931472
Warning message:
production de NaN in: log(x)

I am aware of the section "Warning" of the help page for ifelse:
Sometimes it is better to use a construction such as (tmp <- yes;
tmp[!test] <- no[!test]; tmp), possibly extended to handle missing
values in test.

However, is there a way to avoid the evaluation of the alternative
expression in ifelse when the argument test is false ?

Best regards,

Renaud
-- 
Renaud LANCELOT
Département Elevage et Médecine Vétérinaire (EMVT) du CIRAD
Directeur adjoint chargé des affaires scientifiques

CIRAD, Animal Production and Veterinary Medicine Department
Deputy director for scientific affairs

Campus international de Baillarguet
TA 30 / B (Bât. B, Bur. 214)
34398 Montpellier Cedex 5 - France
Tél   +33 (0)4 67 59 37 17
Secr. +33 (0)4 67 59 39 04
Fax   +33 (0)4 67 59 37 95



More information about the R-help mailing list