R中的5分钟间隔时间序列

时间:2018-02-13 16:03:53

标签: r time-series

我创建的数据集的日期从2010-01-01到2010-08-1,间隔为5分钟。

library(chron)
t1 <- chron("2010/01/01","00:00:00",format=c("y/m/d","h:m:s"))
t2 <- chron("2018/01/01","00:00:01",format=c("y/m/d","h:m:s"))
deltat <- times("00:05:00")
date <- seq(t1,t2,by=times("00:05:00"))

我使用解析包来格式化日期以遵循ISO 8601标准,将时区更改为GMT + 2.

library(parsedate)
date1 <- format_iso_8601(as.POSIXlt(date,tz="Etc/GMT+2"))
head(date1)
[1] "2009-12-31T22:00:00+00:00" "2009-12-31T22:05:00+00:00" "2009-12-31T22:10:00+00:00"
[4] "2009-12-31T22:15:00+00:00" "2009-12-31T22:20:00+00:00" "2009-12-31T22:25:00+00:00"

但是,当我使用View()时:

View(date1)

观察510918后间隔发生变化。我无法理解原因。如果我改变时区也会发生同样的情况。我该如何解决这个问题?

2014-11-09T22:24:59+00:00
2014-11-09T22:30:00+00:00
2014-11-09T22:35:00+00:00
2014-11-09T22:40:00+00:00
2014-11-09T22:45:00+00:00
2014-11-09T22:50:00+00:00
2014-11-09T22:55:00+00:00
2014-11-09T23:00:00+00:00
2014-11-09T23:05:00+00:00
2014-11-09T23:09:59+00:00
2014-11-09T23:15:00+00:00
2014-11-09T23:20:00+00:00
2014-11-09T23:25:00+00:00
2014-11-09T23:30:00+00:00
2014-11-09T23:35:00+00:00
2014-11-09T23:40:00+00:00
2014-11-09T23:45:00+00:00
2014-11-09T23:50:00+00:00
2014-11-09T23:54:59+00:00
2014-11-10T00:00:00+00:00
2014-11-10T00:05:00+00:00
2014-11-10T00:10:00+00:00
2014-11-10T00:15:00+00:00
2014-11-10T00:20:00+00:00
2014-11-10T00:25:00+00:00
2014-11-10T00:30:00+00:00
2014-11-10T00:35:00+00:00
2014-11-10T00:39:59+00:00

1 个答案:

答案 0 :(得分:1)

您是否只想要一个时间间隔为5分钟的日期序列?如果是这样,您可以使用here

所述的seq.PosiXt

请参阅下面的示例:

start_date <- as.POSIXct(paste0("2010/01/01", "00:00:00"), format= "%Y/%m/%d %H:%M:%S", tz = "Etc/GMT+2")
end_date <- as.POSIXct(paste0("2018/01/01","00:00:01"), format= "%Y/%m/%d %H:%M:%S", tz = "Etc/GMT+2")


output <- seq.POSIXt(start_date, end_date, by = "5 min")
formatted <- format_iso_8601(output)