[英]R: NaNs produced
I have a time series in R Studio.我在 R Studio 中有一个时间序列。 Now I want to calculate the log() of this series.
现在我想计算这个系列的 log()。 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:但是后来我得到以下信息:
Warning message: In log(i): NaNs produced
为了检查这个我使用了: table(is.nan(ii))
它给了我以下输出:
FALSE TRUE
2480 1
So I assume, that there is 1 NaN in my time series now.所以我假设,现在我的时间序列中有 1 个 NaN。 My question is: what code can I use, that R shows me for which observation a NaN was produced?
我的问题是:我可以使用什么代码,R 向我展示了 NaN 产生的观察结果? 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)?这是一个小数据样本:
i <- c(9,8,4,5,7,1,6,-1,8,4)
顺便说一句,我如何在i <- c(9,8,4,5,7,1,6,-1,8,4)
键入数学公式,例如 log(x) ? Many thanks非常感谢
As I said in my comment, to know which observation generated the NaN
, you can use function which
:正如我在评论中所说,要知道哪个观察生成了
NaN
,您可以使用函数which
:
i <- c(9,8,4,5,7,1,6,-1,8,4)
which(is.nan(log(i))) # 8
Use the test to subset your original vector with the values that produce NaN:使用测试用产生 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
.在这里您可以看到 -7、-1 和
-Inf
生成了NaN
。
Note that log(NA)
is not NaN
, its NA
, which is a different sort of not-numberness.请注意,
log(NA)
不是NaN
,而是它的NA
,这是一种不同的非数字。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.