Anthony Damico Anthony Damico - 1 month ago 18
R Question

how to calculate a weighted.mean with plyr on all columns in a data.frame?

just curious if there's a sneaky way to do this that i'm missing.

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)


unweighted mean comparisons

# 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))


not sure how to do this with
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?


thanks all

Answer

Here is a dplyr solution, hope this helps

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