【发布时间】:2016-02-15 00:24:07
【问题描述】:
我需要计算每月加权平均值。数据框如下所示:
Month Variable Weighting
460773 1998-06-01 11 153.00
337134 1998-06-01 9 0.96
473777 1998-06-01 10 264.00
358226 1998-06-01 6 0.52
414626 1998-06-01 10 34.00
341020 1998-05-01 9 1.64
453066 1998-05-01 5 26.00
183276 1998-05-01 8 0.51
403729 1998-05-01 6 123.00
203005 1998-05-01 11 0.89
当我使用aggregate 例如,
Output <- aggregate(Variable ~ Month, df , mean )
Output
Month Variable
1 1998-05-01 7.8
2 1998-06-01 9.2
但是,当我尝试向聚合添加权重时,我得到了正确的结果,例如,
Output <- aggregate(Variable ~ Month, df , FUN = weighted.mean, w = df$Weighting)
我得到一个不同向量长度的错误:
Error in weighted.mean.default(X[[1L]], ...) :
'x' and 'w' must have the same length
有没有办法补救这种情况?
【问题讨论】:
标签: r time-series aggregate