R xts:索引为.001毫秒

时间:2012-03-20 13:16:00

标签: r xts

看起来POSIXlt允许毫秒精度规范,但是在xts对象中设置.001毫秒索引值时遇到问题:

> options(digits.secs = 3)
> data(sample_matrix)
> sample.xts = xts(sample_matrix, rep(as.POSIXlt("2012-03-20 09:02:50.001"), 180))
> head(sample.xts, 10)
                            Open     High      Low    Close
2012-03-20 09:02:50.000 50.03978 50.11778 49.95041 50.11778
2012-03-20 09:02:50.000 50.23050 50.42188 50.23050 50.39767
2012-03-20 09:02:50.000 50.42096 50.42096 50.26414 50.33236
2012-03-20 09:02:50.000 50.37347 50.37347 50.22103 50.33459
2012-03-20 09:02:50.000 50.24433 50.24433 50.11121 50.18112
2012-03-20 09:02:50.000 50.13211 50.21561 49.99185 49.99185
2012-03-20 09:02:50.000 50.03555 50.10363 49.96971 49.98806
2012-03-20 09:02:50.000 49.99489 49.99489 49.80454 49.91333
2012-03-20 09:02:50.000 49.91228 50.13053 49.91228 49.97246
2012-03-20 09:02:50.000 49.88529 50.23910 49.88529 50.23910
> sample.xts = xts(sample_matrix, rep(as.POSIXlt("2012-03-20 09:02:50.002"), 180))
> head(sample.xts, 10)
                            Open     High      Low    Close
2012-03-20 09:02:50.002 50.03978 50.11778 49.95041 50.11778
2012-03-20 09:02:50.002 50.23050 50.42188 50.23050 50.39767
2012-03-20 09:02:50.002 50.42096 50.42096 50.26414 50.33236
2012-03-20 09:02:50.002 50.37347 50.37347 50.22103 50.33459
2012-03-20 09:02:50.002 50.24433 50.24433 50.11121 50.18112
2012-03-20 09:02:50.002 50.13211 50.21561 49.99185 49.99185
2012-03-20 09:02:50.002 50.03555 50.10363 49.96971 49.98806
2012-03-20 09:02:50.002 49.99489 49.99489 49.80454 49.91333
2012-03-20 09:02:50.002 49.91228 50.13053 49.91228 49.97246
2012-03-20 09:02:50.002 49.88529 50.23910 49.88529 50.23910

为什么001毫秒设置失败?

2 个答案:

答案 0 :(得分:4)

POSIXct表示是Ripley教授非常聪明的“黑客”,他将一个标准的双字节单词分解为“自纪元以来通常的'天数'的”足够“表示,加上'适当的精确度亚秒数据'。它可以达到大约一微秒:

R> now <- Sys.time()
R> for (x in seq(1,10)) print(difftime(now, now + 10^-x))
Time difference of -0.0999999 secs
Time difference of -0.00999999 secs
Time difference of -0.000999928 secs
Time difference of -9.98974e-05 secs
Time difference of -1.00136e-05 secs
Time difference of -9.53674e-07 secs
Time difference of 0 secs
Time difference of 0 secs
Time difference of 0 secs
Time difference of 0 secs
R>

答案 1 :(得分:2)

我怀疑这是一个舍入/浮点问题:

Browse[2]> print(head(as.numeric(order.by)), digits = 20)
[1] 1332234170.0009999275 1332234170.0009999275 1332234170.0009999275
[4] 1332234170.0009999275 1332234170.0009999275 1332234170.0009999275

这是通过调用{/ p>上的xts()来实现的

foo <- xts(1:180, rep(as.POSIXlt("2012-03-20 09:02:50.001"), 180), 
           unqiue = FALSE)

但您可以通过

清楚地看到问题
> print(as.numeric(as.POSIXlt("2012-03-20 09:02:50.001")))
[1] 1332234170
> print(as.numeric(as.POSIXlt("2012-03-20 09:02:50.001")), digits = 20)
[1] 1332234170.0009999275

指示您的小数秒数不能创建,也不能以.001毫秒的时间存储。截断为3 dp将保留.002,因为它存储为:

> print(as.numeric(as.POSIXlt("2012-03-20 09:02:50.002")), digits = 20)
[1] 1332234170.0020000935

截断或舍入到3 dp将保留.002部分。使用计算机时必须解决的一个问题。

请注意,这似乎只是索引日期的打印表示中的一个问题:

> print(as.numeric(index(foo)[1]), digits = 20)
[1] 1332234170.0009999275

精度(浮点问题)保存在存储索引时间的实际对象中 - 在将时间打印到控制台时,您无法看到它。