2
votes

I have a time series in R Studio. Now I want to calculate the log() of this series. I tried the following:

i <- (x-y) 
ii <- log(i)

But then I get the following: Warning message: In log(i): NaNs produced To inspect this I used: table(is.nan(ii)) which gives me the following output:

FALSE  TRUE 
 2480     1 

So I assume, that there is 1 NaN in my time series now. My question is: what code can I use, that R shows me for which observation a NaN was produced? Here is a small data sample: i <- c(9,8,4,5,7,1,6,-1,8,4) Btw how do I type mathematical formulas in stackoverflow, for example for log(x)? Many thanks

2
you can't take the log of negative numbers - Dason
which is probably the function you are looking for : which(is.nan(log(i))) # 8 - Cath

2 Answers

8
votes

As I said in my comment, to know which observation generated the NaN, you can use function which:

i <- c(9,8,4,5,7,1,6,-1,8,4)
which(is.nan(log(i))) # 8
3
votes

Use the test to subset your original vector with the values that produce NaN:

> i <- c(9,8,4,5,-7,1,6,-1,8,4,Inf,-Inf,NA)
> i[which(is.nan(log(i)))]
[1]   -7   -1 -Inf
Warning message:
In log(i) : NaNs produced

Here you see that -7, -1, and -Inf produced NaN.

Note that log(NA) is not NaN, its NA, which is a different sort of not-numberness.