问题
I have a problem in generating proper time sequence using R:
fivemin <- seq(as.POSIXct("2014/01/01 0:00:00"), as.POSIXct("2014/04/01 0:00:00"), by="5 mins",tz="EST")
time <- data.frame(MasterTime=fivemin)
Using above code, I can get a data frame with 25909 observations. However, under Eastern Standard Time (without daylight saving), the number of observations should be 25920. The difference is 1 hour from the transition of daylight saving time on 03/09/20014, because then the time would change from 1 AM to 3 AM directly. I'm not sure how R deals with this kind of time change. How can I revise my code so R produce a time sequence without that missing 2AM hour on 03/09/2014? Does anyone have any idea on this?
Really thanks!
回答1:
If you don't want to use daylight saving time try setting tz = 'UTC'
fivemin <- seq(as.POSIXct("2014/01/01 0:00:00", tz = 'UTC'), as.POSIXct("2014/04/01 0:00:00", tz = 'UTC'), by="5 mins")
head(fivemin)
length(fivemin)
#[1] 25921
Apart from 'UTC' or 'GMT' it's better not to use the three letter designation for a timezone e.g "EST" because these are ambiguous. Australia has an Eastern Standard Time, as does the US. Instead use Country/City
fivemin <- seq(as.POSIXct("2014-04-06 2:00:00", tz="Australia/Melbourne"), as.POSIXct("2014-04-06 3:00:00", tz="Australia/Melbourne"), by="5 mins")
length(fivemin)
# [1] 25
# Melbourne's DST finished on 6 April 2014 at 3 am so there are 25 x 5 min periods in the hour between 2am and 3 am
fivemin <- seq(as.POSIXct("2014-04-06 2:00:00", tz="Australia/Brisbane"), as.POSIXct("2014-04-06 3:00:00", tz="Australia/Brisbane"), by="5 mins")
length(fivemin)
# [1] 13 Brisbane doesn't use DST so no problem
Also check the dst function in the lubridate package.
If you would like to retain the time zone, but use standard time, try something like.
library(lubridate)
fivemin <- seq(as.POSIXct("2014-04-06 2:00:00", tz="Australia/Melbourne"), as.POSIXct("2014-04-06 3:00:00", tz="Australia/Melbourne"), by="5 mins")
fivemin[dst(fivemin)] <- fivemin[dst(fivemin)]-3600
来源:https://stackoverflow.com/questions/23611556/missing-hour-in-seq-function-generating-time-in-r