如何解析毫秒?

问题描述 投票:0回答:2

如何使用

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"`
r datetime time-series strptime
2个回答
132
投票

礼貌

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

> 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

31
投票

您也可以使用

strptime(time[1], "%OSn")
其中 0 <= n <= 6, without having to set
digits.secs
.

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

© www.soinside.com 2019 - 2024. All rights reserved.