如何解析毫秒?

如何使用 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"`
51713 次浏览

Courtesy of the ?strptime help file (with the example changed to your value):

> z <- strptime("2010-01-15 13:55:23.975", "%Y-%m-%d %H:%M:%OS")
> z # prints without fractional seconds
[1] "2010-01-15 13:55:23 UTC"


> op <- options(digits.secs=3)
> z
[1] "2010-01-15 13:55:23.975 UTC"


> options(op) #reset options

You can also use strptime(time[1], "%OSn") where 0 <= n <= 6, without having to set digits.secs.

The documentation states "Which of these are supported is OS-dependent." so YMMV.