vendredi 14 juillet 2017

ifelse shows warning based on the else function even when the test condition is met

I have a vector. For example,

a = c(5,-5)

I am wanting to perform different functions on the value of a depending on whether a>0 or not

For simplicity, let's say the function is as follows

output = ifelse(a>0, log(1+a), 0)

This code returns the desired values in that output is equal to

1.791759 0.000000

However, it also shows the warning:

Warning message:
In log(1+a) : NaNs produced

I was hoping someone might be able to explain why this warning is displaying despite the fact that log(1+a) would only ever be called when a>0 (which would not produce an NaN). Is this a weird quirk of ifelse or am I doing something wrong.

I will also note that this warning does not occur when both elements of a are less than 0 (e.g. a=c(-5,-5))

Aucun commentaire:

Enregistrer un commentaire