毫秒时间戳作为data.table中的键

Ryo*_*ogi 5 datetime r data.table

在这个问题data.table中讨论了在s 中使用日期的问题.解决方案是使用内置类来表示时间和日期.这些工作精度高达秒.在索引列中是否有处理毫秒的解决方法?

Dir*_*tel 8

日期和时间的内置类,例如POSIXct毫秒(窗口)和微秒(Linux,OS X).您可能没有打开选项以打印亚秒:

R> Sys.time()                            ## under default options
[1] "2011-10-25 17:40:05 CDT"
R> options("digits.secs"=7)              ## you may want this in ~/.Rprofile too
R> Sys.time()
[1] "2011-10-25 17:40:11.177271 CDT"     
R> 
Run Code Online (Sandbox Code Playgroud)


小智 4

是的,data.table 要求键为整数或类似值(即 POSIXct 四舍五入到秒)。我会通过存储 1000 * 时间戳作为密钥来解决这个问题,并且可能有一个单独的列,即非舍入的 POSIXct。或者,您可以在需要时即时转换为 POSIXct。

  • 是的,例如 `as.integer(1000*as.numeric(format(Sys.time(),"%H%M%OS3")))` `[1] 15017710`,作为解决方法。当涉及到滚动连接时,将日期和时间放在两列中可能会很好:如果您想要当天的主要记录而不是前一天的记录。 (2认同)