Renaud Lancelot
2006-Jun-04 17:53 UTC
[R] evaluation of the alternative expression in ifelse
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
Dimitrios Rizopoulos
2006-Jun-04 18:15 UTC
[R] evaluation of the alternative expression in ifelse
Quoting Renaud Lancelot <renaud.lancelot at gmail.com>:> 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 ?I think the answer is no; the Details section of ifelse() reads: "... 'yes' will be evaluated if and only if *any* element of test is true, and analogously for 'no'." check also the code behind ifelse(). If you really want to use ifelse (), then you could consider something like: x <- -2:2 opt <- options(warn = -1) ifelse(test = x < 0, yes = NaN, no = log(x)) options(opt) I hope it helps. Best, Dimitris> 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 > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html > >Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm
Gabor Grothendieck
2006-Jun-04 18:15 UTC
[R] evaluation of the alternative expression in ifelse
Try: log(ifelse(x < 0, NaN, x)) or suppressWarnings(ifelse(x < 0, NaN, log(x))) or ifelse(x < 0, NaN, log(pmax(0, x))) On 6/4/06, Renaud Lancelot <renaud.lancelot at gmail.com> wrote:> 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 > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html >