R as.POSIXct parsing error

左心房为你撑大大i 提交于 2019-12-17 14:53:51

问题


I am trying to parse a vector of time string and came across a strange error. For example, if I run the following section of code, R returned the result as expected.

time_format="%m/%d/%Y %H:%M:%S"
t_1 = "03/13/2011 01:00:10"
as.POSIXct(t_1, format = time_format)

Output:

[1] "2011-03-13 01:00:10 EST"

However, if I change the time slightly to 2 AM

t_2 = "03/13/2011 02:00:10"
as.POSIXct(t_2, format = time_format)

The output became:

[1] NA

I can reproduce it on R 2.11.1 and 2.12.2 on Windows 7 and XP. Does anyone encounter the same problem?

Thanks, Derek


回答1:


You cannot parse non-existing times. 02:00:10 did not exist as we had 'spring forward' this Saturday night / Sunday morning with the switch to daylight-savings. R knows this:

R> t_1 = "03/13/2011 01:00:10"; as.POSIXct(t_1, format = time_format)
[1] "2011-03-13 01:00:10 CST"
R> t_2 = "03/13/2011 02:00:10"; as.POSIXct(t_2, format = time_format)
[1] "2011-03-13 01:00:10 CST"
R> t_3 = "03/13/2011 03:00:10"; as.POSIXct(t_3, format = time_format)
[1] "2011-03-13 03:00:10 CDT"
R> 

On Linux, my timezone library seems to cope -- 02:00:10 becomes 01:00:10 as an hour is subtracted.



来源:https://stackoverflow.com/questions/5302893/r-as-posixct-parsing-error

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