R xts: millisecond index

后端 未结 1 621
暖寄归人
暖寄归人 2021-02-10 13:16

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().

<
1条回答
  •  悲哀的现实
    2021-02-10 13:30

    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"
    

    The trick to see the milliseconds is to alter the digits.secs option via options(). The above performed using:

    > getOption("digits.secs")
    [1] 3
    

    Which is set using

    > opts <- options(digits.secs = 3)
    

    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?

    0 讨论(0)
提交回复
热议问题