For a project, I was converting strings to POSIXct
format using strptime
function.
x <- strptime("2016-03-13 02:56:16", "%Y-%m-%d %H:%M:%S")
class(x)
[1] "POSIXlt" "POSIXt"
x + 3600
[1] NA
Although class(x)
results in "POSIXlt" "POSIXt"
, doing x + 3600
, I get NA
If I do a similar procedure for 2017 with the same month, day, hour, minute, second, it works!
x <- strptime("2017-03-13 02:56:16", "%Y-%m-%d %H:%M:%S")
class(x)
[1] "POSIXlt" "POSIXt"
x + 3600
[1] "2017-03-13 03:56:16 EDT"
Looking back at my data, I see a list of dates where there will be a problem in date conversion!
[1] "2016-03-13 02:51:02" "2016-03-13 02:49:46" "2016-03-13 02:24:15" "2016-03-13 02:37:52" "2016-03-13 02:56:16"
[6] "2016-03-13 02:59:32" "2016-03-13 02:49:29" "2016-03-13 02:27:20" "2016-03-13 02:22:20" "2016-03-13 02:44:11"
[11] "2016-03-13 02:43:31" "2016-03-13 02:44:46" "2016-03-13 02:07:48" "2016-03-13 02:36:36" "2016-03-13 02:51:39"
[16] "2016-03-13 02:00:13" "2016-03-13 02:42:37" "2016-03-13 02:48:53" "2016-03-13 02:53:05" "2016-03-13 02:01:41"
[21] "2016-03-13 02:57:36" "2016-03-13 02:40:09" "2016-03-13 02:27:19" "2016-03-13 02:33:08" "2016-03-13 02:18:51"
[26] "2016-03-13 02:05:45" "2016-03-13 02:06:09" "2016-03-13 02:06:09" "2016-03-13 02:03:25" "2016-03-13 02:32:14"
[31] "2016-03-13 02:59:34" "2016-03-13 02:35:39" "2016-03-13 02:51:10" "2016-03-13 02:50:56" "2016-03-13 02:38:56"
[36] "2016-03-13 02:25:35" "2016-03-13 02:20:09" "2016-03-13 02:30:43" "2016-03-13 02:00:04" "2016-03-13 02:51:55"
[41] "2016-03-13 02:26:25" "2016-03-13 02:37:20" "2016-03-13 02:49:29" "2016-03-13 02:35:50" "2016-03-13 02:05:38"
[46] "2016-03-13 02:15:10" "2016-03-13 02:54:23"
If I change the year to 2015
or 2017
, or change the day from 13
to something else, it works. It seems that there is something going on March 13th of 2016. Am I missing something or this is a flaw within R?
I am using R version 3.3.2 (2016-10-31)
The problem is that there was no such time as "2016-03-13 02:56:16". 13 Mar 2016 was when Daylight Savings Time started. At 2AM, that day, the clock jumped immediately to 3AM.
来源:https://stackoverflow.com/questions/42721218/is-r-superstitious-regarding-posixct-data-type