I'm trying to calculate the weighted mean for group variable X1, across all numeric variables, here is some example data
set.seed(123)
X1=rep(c("A", "B", "C"), each = 4)
Y1=as.numeric(seq(1,12,by=1))
Y2=sample(1:5,12,TRUE)
Y3=sample(10:20,12,TRUE)
wgt <- abs(rnorm(12)*10)
df <- data.frame(X1,Y1,Y2,Y3,wgt)
This is the code I've been using to calculate regular mean values for X1
aggregate( df[, sapply(df, is.numeric)] , by=list(df$X1) , FUN=mean, na.rm=TRUE)
I want to calculate weight mean, weight variable is wgt. I tried both of these codes and neither work. I've tried numerous different ways and nothing is working.
aggregate( df[, sapply(df, is.numeric)] , by=list(df$X1) , FUN=weighted.mean(x, w=df$wgt), na.rm = TRUE)
aggregate( df[, sapply(df, is.numeric)] , by=list(df$X1) , FUN=weighted.mean, w=df$wgt, na.rm = TRUE)
I'm unable to adapt the weighted.mean function. Can anyone tell me where I'm going wrong? Can this function even be used in this situation? Any help is greatly appreciated. Thanks