我能够在因子水平内成功得到一个给定向量的简单平均值,但是在试图将其加权到下一步对观测值加权时,我无法使其工作。这工作:如何在因子水平内计算矢量的加权平均值?
> tapply(exp.f,part.f.p.d,mean)
1 2 3 4 5 6 7 8 9 10
0.8535996 1.1256058 0.6968142 1.4346451 0.8136110 1.2006801 1.6112160 1.9168835 1.5135006 3.0312460
但这并不:
> tapply(exp.f,part.f.p.d,weighted.mean,b.pct)
Error in weighted.mean.default(X[[1L]], ...) :
'x' and 'w' must have the same length
>
在下面的代码,我试图找到exp.f的加权平均,因子part.fpd的水平之内,通过b.pct内每个级别的观察值加权。
b.exp <- tapply(exp.f,part.f.p.d,weighted.mean,b.pct)
Error in weighted.mean.default(X[[1L]], ...) :
'x' and 'w' must have the same length
我想我必须提供不正确的语法,因为这些载体的所有3的长度相同:
> length(b.pct)
[1] 978
> length(exp.f)
[1] 978
> length(part.f.p.d)
[1] 978
什么是做这种正确的方法是什么?先谢谢你。
嗨jonw- exp.f是股票预期收益的数字矢量,part.fpd是10个水平的因子,b.pct是指数(前1000名股票)中每只股票的百分比 – user297400 2011-02-01 18:47:09
查看http:/ /堆栈溢出。COM /问题/ 3685492/R-加速部分组按操作。 – Charles 2011-02-01 18:51:58