Colon in date format between seconds and milliseconds. How to parse in R?

风流意气都作罢 提交于 2019-12-08 16:10:08

问题


How can I parse this date format? Should I change this colon to dot or maybe someone know better solution?

> x <- "2012.01.15 09:00:02:002"
> strptime(x, "%Y.%m.%d %H:%M:%S:%OS") 
[1] "2012-01-15 09:00:02"
> strptime(x, "%Y.%m.%d %H:%M:%OS")
[1] "2012-01-15 09:00:02"
> x <- "2012.01.15 09:00:02.002"
> strptime(x, "%Y.%m.%d %H:%M:%OS")
[1] "2012-01-15 09:00:02.001"

回答1:


There's a subtle distinction here that may be throwing you off. As ?strptime notes:

for 'strptime' '%OS' will input seconds including fractional seconds.

To emphasize that a bit, %OS represents the seconds including fractional seconds --- not just the fractional part of the seconds: if the seconds value is 44.234, %OS or %OS3 represents 44.234, not .234

So the solution is indeed to substitute a . for that final :.

Here's one way you might do that:

x <- "2012.01.15 09:00:02:002"
strptime(gsub(":", ".", x), "%Y.%m.%d %H.%M.%OS") 



回答2:


Would

strptime(gsub(":", ".", x), "%Y.%m.%d %H.%M.%OS3")

be cheating?



来源:https://stackoverflow.com/questions/13613655/colon-in-date-format-between-seconds-and-milliseconds-how-to-parse-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!