如何在data.frame中的所有列上使用plyr计算weighted.mean?

时间:2016-10-05 18:32:20

标签: r data.table plyr

只是好奇,如果有一种偷偷摸摸的方式来做这件事,我就错过了。

library(plyr)
library(data.table)

dfx <- data.frame(
  group = c(rep('A', 8), rep('B', 15), rep('C', 6)),
  sex = sample(c("M", "F"), size = 29, replace = TRUE),
  age = runif(n = 29, min = 18, max = 54) ,
  score = runif( n = 29 ) ,
  weight = sample( 0:1 , 29 , replace = TRUE )
)

dt_dfx <- as.data.table(dfx)

未加权平均比较

# mean of all columns not specified in by=
dt_dfx[ , lapply( .SD , mean ) , by = .(sex,group) ]

# here's how to match the data.table unweighted mean
ddply(dfx, .(group,sex), numcolwise(mean))

不确定如何使用plyr

执行此操作
# weighted.mean of all columns not specified in by=
dt_dfx[ , lapply( .SD , weighted.mean , weight ) , by = .(sex,group) ]

# easy way to match the data.table weighted.mean?

感谢所有

1 个答案:

答案 0 :(得分:1)

这是一个dplyr解决方案,希望这有帮助

dfx %>%
group_by( sex , group ) %>%
summarize_each( funs( weighted.mean( . , weight ) ) , -weight )