With
xa=aggregate(x$avg,by=list(x$value),FUN=weighted.mean,w=x$weight)
给我一个错误
Error in weighted.mean.default(X[[1L]], ...) : 'x' and 'w' must
have the same length
But
weighted.mean(x$avg,w=x$weight);
工作正常。
正如建议的旧R螺纹 http://tolstoy.newcastle.edu.au/R/help/03a/7057.html, 您可以使用by
反而:
wt <- c(5, 5, 4, 1)/15
x <- c(3.7,3.3,3.5,2.8)
xx <- data.frame(avg=x, value=gl(2,2), weight=wt)
by(xx, xx$value, function(x) weighted.mean(x$avg, x$weight))
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)