#### grouped weighted mean in r (preferably with ddply but whatever works)

``````mydf= structure(list(group = structure(c(1L, 2L, 1L, 2L, 1L), .Label = c("a","b"), class = "factor"),
price = c(15, 20, 10, 40, 20), size = c(100, 10, 50, 50, 1000),
gain = c(0.03, 0.02, 0.05, 0.1, 0.01), wt = c(1500, 200, 500, 2000, 20000)),
.Names = c("group", "price", "size", "gain", "wt"), row.names = c(NA, -5L),
class = "data.frame")
mydf
library(plyr)
ddply(x, .(group), summarise,normal_mean= mean(gain),
wt_mean= weighted.mean(x\$price*x\$size,x\$gain))
``````

2 个回复

``````library(data.table)

setDT(mydf)[,list(normalMean=mean(gain),
weightedMean=weighted.mean(gain, wt/sum(wt))),
by = group]

#   group normalMean weightedMean
#1:     a       0.03   0.01227273
#2:     b       0.06   0.09272727
``````

`dplyr`的方法

``````mydf %>% group_by(group) %>%
summarise (mean=mean(gain), avgwt = weighted.mean(gain,wt))

group mean      avgwt
1     a 0.03 0.01227273
2     b 0.06 0.09272727
``````
1 R ddply和加权平均值

2013-06-20 16:42:01 1 746   r
2 在R中分组，ddply与weighted.mean

3 ddply的加权平均值是错误的（R，ddply）

2016-09-14 02:44:28 1 104   r
4 R中的weighted.mean命令很奇怪

5 R与ddply的总和和平均值

2015-11-13 22:00:56 7 5180   r/ plyr
6 R加权算术平均值

7 加权平均 R

8 编码加权平均值（R）

9 R中的聚合和加权平均值

10 R中具有加权平均值的aggregate（）