R xts:毫秒索引 [英] R xts: millisecond index
问题描述
如何创建索引包含毫秒的 xts 对象?我在 POSIXlt 帮助页面中找不到任何格式规范,但有一个 参考 到 indexFormat() 中的 %OS.
How do I create an xts object whose indexes include milliseconds? I can't find any format spec in the POSIXlt help page but there is a reference to %OS in indexFormat().
更新
基于 Gavin 动物园答案的 xts 示例:
xts example based on Gavin zoo answer:
> options(digits.secs = 3)
> data(sample_matrix)
> sample.xts = xts(sample_matrix, Sys.time() + seq(0, by = 0.1, length = 180))
> head(sample.xts)
Open High Low Close
2012-03-20 08:49:02.820 50.03978 50.11778 49.95041 50.11778
2012-03-20 08:49:02.920 50.23050 50.42188 50.23050 50.39767
2012-03-20 08:49:03.020 50.42096 50.42096 50.26414 50.33236
2012-03-20 08:49:03.120 50.37347 50.37347 50.22103 50.33459
2012-03-20 08:49:03.220 50.24433 50.24433 50.11121 50.18112
2012-03-20 08:49:03.320 50.13211 50.21561 49.99185 49.99185
推荐答案
这适用于 zoo 包,所以我怀疑它也适用于 xts 因为后者建立在以前.
This works with package zoo so I suspect it works also with xts as the latter builds upon the former.
> ## create some times with milliseconds
> times <- Sys.time() + seq(0, by = 0.1, length = 10)
> times
[1] "2012-03-19 22:10:57.763 GMT" "2012-03-19 22:10:57.863 GMT"
[3] "2012-03-19 22:10:57.963 GMT" "2012-03-19 22:10:58.063 GMT"
[5] "2012-03-19 22:10:58.163 GMT" "2012-03-19 22:10:58.263 GMT"
[7] "2012-03-19 22:10:58.363 GMT" "2012-03-19 22:10:58.463 GMT"
[9] "2012-03-19 22:10:58.563 GMT" "2012-03-19 22:10:58.663 GMT"
> ZOO <- zoo(1:10, order = times)
> index(ZOO)
[1] "2012-03-19 22:10:57.763 GMT" "2012-03-19 22:10:57.863 GMT"
[3] "2012-03-19 22:10:57.963 GMT" "2012-03-19 22:10:58.063 GMT"
[5] "2012-03-19 22:10:58.163 GMT" "2012-03-19 22:10:58.263 GMT"
[7] "2012-03-19 22:10:58.363 GMT" "2012-03-19 22:10:58.463 GMT"
[9] "2012-03-19 22:10:58.563 GMT" "2012-03-19 22:10:58.663 GMT"
查看毫秒的技巧是通过 options()
更改 digits.secs
选项.以上执行使用:
The trick to see the milliseconds is to alter the digits.secs
option via options()
. The above performed using:
> getOption("digits.secs")
[1] 3
使用什么设置
> opts <- options(digits.secs = 3)
您可以通过执行 options(opts)
将其重置为默认值 (0).默认情况下,R 不打印亚秒信息,因为 digits.secs
默认为 0
.即使没有打印,数据也会以亚秒级精度记录.
You can reset this to default (0) by doing options(opts)
. By default R doesn't print sub-second information because digits.secs
defaults to 0
. The data are recorded to sub-second accuracy though, even if not printed.
如果这不是你的意思,你能解释一下你做了什么不起作用的事情吗?
If this is not what you meant, can you explain what you did that was not working?
这篇关于R xts:毫秒索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!