时间序列的15分钟时间段

时间:2016-07-06 16:18:01

标签: r time-series xts

我有下面的代码块,我试图填写我的数据df_stuff中缺少的分钟数,将其加入到一个包含一整年所有分钟的时间序列中。我实际上想以15分钟而不是分钟的方式聚合这些数据。有谁知道这样做的简单方法?我从xts包中查看to.minutes15,但我的POSIXct格式时间序列似乎有问题。

代码:

library("sqldf")

##Filling Gaps in time by minute
myTZ <- "America/Los_Angeles"
tseries <- seq(as.POSIXct("2015-01-01 00:00:00", tz=myTZ),
               as.POSIXct("2015-12-31 23:59:00", tz=myTZ), by="min")

df2 <- data.frame(SeqDateTime=tseries)
finaldf <- sqldf("select df2.SeqDateTime,
                  median(df_stuff.brooms) as broomsTot
                  from df2
                  left outer join df_stuff on df2.SeqDateTime = df_stuff.broomTime
                  group by df2.SeqDateTime
                  order by df2.SeqDateTime asc")

数据:

df_stuff <- structure(list(brooms = c(27, 53, 10, 55, 14, 49, 26, 
13, 12, NA, NA, 23, 28, 31, NA, 46, NA, 13, NA, 33, 12, 4, 28, 
34, 0, 24, 7, 31, 33, 37, 56, 41, 50, 55, 41, 15, 23, 26, 14, 
27, 22, 41, 48, 19, 28, 11, 11, NA, 49, NA), broomTime =   structure(c(1423970100, 
1424122200, 1424136180, 1424035260, 1424141580, 1424122440, 1423274580, 
1424129580, 1424146320, 1429129320, 1429032060, 1429142940, 1428705000, 
1429142460, 1429128720, 1429204560, 1422909480, 1424137200, 1424042100, 
1424149620, 1424131920, 1424108940, 1424144820, 1424040600, 1424119620, 
1424148660, 1443593040, 1443657120, 1424125860, 1424223120, 1424235240, 
1424232720, 1424234940, 1424234640, 1424230440, 1424115300, 1429208280, 
1429131720, 1429148460, 1429151040, 1424129760, 1424125380, 1424123220, 
1424137380, 1424115780, 1424219340, 1424131560, 1424233560, 1424224920, 
1443640800), class = c("POSIXct", "POSIXt"), tzone = "")), .Names =   c("brooms", 
"broomTime"), row.names = c(NA, 50L), class = "data.frame")

2 个答案:

答案 0 :(得分:0)

您可以在cut的{​​{1}}函数中使用group_by按任意时间间隔汇总。

dplyr

答案 1 :(得分:0)

我可以向您保证,xts对您的POSIXct时间序列没有问题。 xts使用POSIXct作为其内部时间索引。

以下是如何将df_stuff加入1分钟的剧集,然后将该结果汇总到15分钟的剧集中。

library(xts)
# create xts object
xts_stuff <- with(df_stuff, xts(brooms, broomTime))
# merge with empty xts object that contains a regular 1-minute index
xts_stuff_1min <- merge(xts_stuff, xts(,tseries))
# aggregate to 15-minutes
ep15 <- endpoints(xts_stuff_1min, "minutes", 15)
final_df <- period.apply(xts_stuff_1min, ep15, median, na.rm=TRUE)