POSIXct日期转换错误 [英] POSIXct date conversion error
问题描述
将字符格式的一组日期转换为POSIXct对象时,遇到以下错误。
I've encountered the following error when converting a set of dates in character format to a POSIXct object.
示例数据:
t<-c("3/11/2007 1:30", "3/11/2007 2:00", "4/11/2007 2:00")
str(t)
chr [1:3] "3/11/2007 1:30" "3/11/2007 2:00" "4/11/2007 2:00"
z<-as.POSIXct(strptime(t, format ="%m/%d/%Y %H:%M"))
z
"2007-03-11 01:30:00 MST" NA "2007-04-11 02:00:00 MDT"
str(z)
POSIXct[1:3], format: "2007-03-11 01:30:00" NA "2007-04-11 02:00:00"
我的问题是为什么是z中第二个日期返回的NA吗?我有一个数据集包含8年的小时数据(从中我复制了上面的日期),这个NA错误仅在3/8 - 3/14之间的时间弹出,而当时间是02:00:00时才会弹出。
My question is why is the NA returned for the second date in z? I have a dataset that contains 8 years of hourly data (from which I copied the dates above), and this NA error pops up only for dates between 3/8 - 3/14 and ONLY when the hour is 02:00:00.
如果日期转换为POSIXlt,我不会遇到错误,所以这是我目前的工作。
I do not encounter an error if the dates are converted to POSIXlt, so that is my current work around.
任何想法?
推荐答案
尝试使用一个时区不使用夏令时:
Try using a time zone that does not use daylight savings time:
as.POSIXct(t, format = "%m/%d/%Y %H:%M", tz = "GMT")
## [1] "2007-03-11 01:30:00 GMT" "2007-03-11 02:00:00 GMT" "2007-04-11 02:00:00 GMT"
这篇关于POSIXct日期转换错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!