嗨,我有一个xts对象,它有4个变量(2个id vars和2个度量):
> head(mi_xts)
squareId country smsIN smsOUT
2013-12-01 00:00:00 9999 39 0.4953734 0.93504713
2013-12-01 00:10:00 9999 39 0.1879042 0.50057622
2013-12-01 00:20:00 9996 39 0.5272736 0.25643745
2013-12-01 00:30:00 9996 39 0.0965593 0.25249854
2013-12-01 00:40:00 9999 39 1.2104980 0.49123277
2013-12-01 00:50:00 9999 39 0.4756599 0.09913715我想使用一个period.apply,它每天返回squareId (我不关心国家)的smsIN和smsOUT组的平均值。我刚写了这段代码:
days <- endpoints(mi_xts, on = "days")
mi_xts.1d<- period.apply(mi_xts, INDEX = days, FUN = mean)但很明显,我只得到了一排的结果:
squareId country smsIN smsOUT
2013-12-01 23:50:00 9995.5 39 0.8418086 0.6644908有什么建议吗?
发布于 2017-04-04 13:33:34
您需要通过split通过"squareId"进行聚合,使用apply.daily进行聚合,然后将所有内容重新组合在一起。
s <- split(mi_xts, mi_xts$squareId)
a <- lapply(s, function(x) apply.daily(x, mean))
r <- do.call(rbind, a)https://stackoverflow.com/questions/43208443
复制相似问题