How can I compute the weighted average of all the fields in a dataset using summarise_each in dplyr? For example, let's say we want to group the mtcars dataset by cyl and compute the weighted average of all columns where the weights are taken as the gear column. I've tried the following but could not get it to work.
mtcars %>% group_by(cyl) %>% summarise_each(funs(weighted.mean(., gear)))
# The line above gives the following output
# Error in weighted.mean.default(c(1, 2, 2, 1, 2, 1, 1, 1, 2, 2, 2), 4.15555555555556) :
# 'x' and 'w' must have the same length
Thanks in advance for your help!