在 xts 中按 period.apply() 分组
Group by period.apply() in xts
您好,我有一个包含 4 个变量(2 个 ID 变量和 2 个度量)的 xts 对象:
> 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 的平均值 returns。
我刚刚写了这段代码:
days <- endpoints(mi_xts, on = "days")
mi_xts.1d<- period.apply(mi_xts, INDEX = days, FUN = mean)
但显然我只得到 1 行结果:
squareId country smsIN smsOUT
2013-12-01 23:50:00 9995.5 39 0.8418086 0.6644908
有什么建议吗?
您需要 split
"squareId"
,使用 apply.daily
聚合,然后 rbind
将所有内容重新组合在一起。
s <- split(mi_xts, mi_xts$squareId)
a <- lapply(s, function(x) apply.daily(x, mean))
r <- do.call(rbind, a)
您好,我有一个包含 4 个变量(2 个 ID 变量和 2 个度量)的 xts 对象:
> 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 的平均值 returns。 我刚刚写了这段代码:
days <- endpoints(mi_xts, on = "days")
mi_xts.1d<- period.apply(mi_xts, INDEX = days, FUN = mean)
但显然我只得到 1 行结果:
squareId country smsIN smsOUT
2013-12-01 23:50:00 9995.5 39 0.8418086 0.6644908
有什么建议吗?
您需要 split
"squareId"
,使用 apply.daily
聚合,然后 rbind
将所有内容重新组合在一起。
s <- split(mi_xts, mi_xts$squareId)
a <- lapply(s, function(x) apply.daily(x, mean))
r <- do.call(rbind, a)