[英]NA for 1 particular date when converting dates from "character" format to "POSIXct" with as.POSIXct
I'm converting a string vector to date format with as.POSIXct().我正在使用 as.POSIXct() 将字符串向量转换为日期格式。 Here is the strange thing:这是奇怪的事情:
as.POSIXct("2017-03-26 03:00:00.000",format="%Y-%m-%d %H")
#Gives
"2017-03-26 03:00:00 CEST"
#While
as.POSIXct("2017-03-26 02:00:00.000",format="%Y-%m-%d %H")
#Outputs
NA
This is really confusing and frustrating.这真的令人困惑和沮丧。 It seem like the function really doesn't like the specific time: 02:00:00.000看起来这个功能真的不喜欢具体时间:02:00:00.000
We can specify the %T
for time.我们可以指定时间的%T
。 In the format, there are minutes, seconds and millseconds.在格式中,有分钟、秒和毫秒。 So, the %H
is only matching the hour part所以, %H
只匹配小时部分
as.POSIXct("2017-03-26 02:00:00.000",format="%Y-%m-%d %T")
[1] "2017-03-26 02:00:00 EDT"
Or to take care of the milliseconds as well或者也照顾毫秒
as.POSIXct("2017-03-26 02:00:00.000",format="%Y-%m-%d %H:%M:%OS")
#[1] "2017-03-26 02:00:00 EDT"
Or using lubridate
或者使用lubridate
library(lubridate)
ymd_hms("2017-03-26 02:00:00.000")
This was a daylight savings issue, the time:这是一个夏令时问题,时间:
"2017-03-26 02:00:00.000"
does not exist in Sweden as we lost an hour this date when changing to "summer time". "2017-03-26 02:00:00.000"
在瑞典不存在,因为我们在更改为“夏令时”时损失了一个小时。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.