如何在因子水平内计算向量的加权平均值?

use*_*400 3 r

我能够在因子水平内成功获得给定向量的简单均值,但是在尝试将其用于加权观察的下一步时,我无法使其工作.这有效:

> 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 
Run Code Online (Sandbox Code Playgroud)

但这不是:

> 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
> 
Run Code Online (Sandbox Code Playgroud)

在下面的代码中,我试图找到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
Run Code Online (Sandbox Code Playgroud)

我想我必须提供不正确的语法,因为所有这三个向量都是相同的长度:

> length(b.pct)
[1] 978
> length(exp.f)
[1] 978
> length(part.f.p.d)
[1] 978
Run Code Online (Sandbox Code Playgroud)

这样做的正确方法是什么?先感谢您.

Jos*_*ich 8

现在我这样做(感谢Gavin):

sapply(split(Data,Data$part.f.p.d), function(x) weighted.mean(x$exp.f,x$b.pct)))
Run Code Online (Sandbox Code Playgroud)

其他人可能会使用ddplyplyr包:

ddply(Data, "part.f.p.d", function(x) weighted.mean(x$exp.f, x$b.pct))
Run Code Online (Sandbox Code Playgroud)