在 POSIXct 中使用亚秒时,我很难按顺序发生序列。
options(digits.secs=6)
x <- xts(1:10, as.POSIXct("2011-01-21") + c(1:10)/1e3)
产生以下输出,为什么时间不按顺序排列?
[,1]
2011-01-21 00:00:00.000 1
2011-01-21 00:00:00.002 2
2011-01-21 00:00:00.003 3
2011-01-21 00:00:00.003 4
2011-01-21 00:00:00.005 5
2011-01-21 00:00:00.006 6
2011-01-21 00:00:00.006 7
2011-01-21 00:00:00.007 8
2011-01-21 00:00:00.009 9
2011-01-21 00:00:00.009 10
我期望下面的代码产生相同的输出
c(1:10)/1e3
[1] 0.001 0.002 0.003 0.004 0.005 0.006 0.007 0.008 0.009 0.010
@GSee是对的,这是一个浮点算术问题。和加文·辛普森的 https://stackoverflow.com/users/429846/gavin-simpson answer https://stackoverflow.com/a/9787993/271616是正确的,因为对象就是这样的printed.
R> options(digits=17)
R> .index(x)
[1] 1295589600.0009999 1295589600.0020001 1295589600.0030000 1295589600.0039999
[5] 1295589600.0050001 1295589600.0060000 1295589600.0070000 1295589600.0079999
[9] 1295589600.0090001 1295589600.0100000
所有的精度都在那里,但是这些线format.POSIXlt
cause options(digits.secs=6)
不被尊重。
np <- getOption("digits.secs")
if (is.null(np))
np <- 0L
else
np <- min(6L, np)
if (np >= 1L) {
for (i in seq_len(np) - 1L) {
if (all(abs(secs - round(secs, i)) < 1e-06)) {
np <- i
break
}
}
}
由于精度问题,在您的示例中np
上面重置为3for
环形。以及格式"%Y-%m-%d %H:%M:%OS3"
产生您发布的时间。如果您使用,您可以看到时间是准确的"%Y-%m-%d %H:%M:%OS6"
format.
R> format(as.POSIXlt(index(x)[1:2]), "%Y-%m-%d %H:%M:%OS3")
[1] "2011-01-21 00:00:00.000" "2011-01-21 00:00:00.002"
R> format(as.POSIXlt(index(x)[1:2]), "%Y-%m-%d %H:%M:%OS6")
[1] "2011-01-21 00:00:00.000999" "2011-01-21 00:00:00.002000"
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)