88

如何使用strptime或任何其他函数来解析 R 中毫秒的时间戳?

time[1]
# [1] "2010-01-15 13:55:23.975"
strptime(time[1], format="%Y-%m-%d %H:%M:%S.%f")
# [1] NA
strptime(time[1], format="%Y-%m-%d %H:%M:%S")
# [1] "2010-01-15 13:55:23"`
4

2 回答 2

126

?strptime帮助文件提供(示例更改为您的值):

 z <- strptime("2010-01-15 13:55:23.975", "%Y-%m-%d %H:%M:%OS")
 z # prints without fractional seconds
 op <- options(digits.secs=3)
 z
 options(op) #reset options
于 2010-01-27T20:55:08.373 回答
31

您也可以使用strptime(time[1], "%OSn")where 0 <= n <= 6,而无需设置digits.secs.

文档指出“其中哪些受支持取决于操作系统。” 所以YMMV。

于 2010-01-27T21:24:21.240 回答