R as.POSIXct 解析错误

发布于 2024-10-22 19:13:50 字数 502 浏览 1 评论 0原文

我正在尝试解析时间字符串向量并遇到一个奇怪的错误。例如,如果我运行以下代码部分,R 将按预期返回结果。

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

输出:

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

但是,如果我将时间稍微更改为凌晨 2 点,

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

输出将变为:

[1] NA

我可以在 Windows 7 和 XP 上的 R 2.11.1 和 2.12.2 上重现它。有人遇到同样的问题吗?

谢谢, 德里克

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冰之心 2024-10-29 19:13:50

无法解析不存在的时间。 02:00:10 不存在,因为我们在本周六晚上/周日早上切换到夏令时,“向前迈进”。 R 知道这一点:

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> 

在 Linux 上,我的时区库似乎可以应付——减去一小时后,02:00:10 变成 01:00:10。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文