zqin
zqin

Reputation: 95

How to fix the error "'x' and 'w' must have the same length" in summarise_all()?

I want to use dplyr::summarise_all() and weighted.mean to calculate the weighted averages of many columns for each group.

I tried to directly use anonymous function, but it returned an error: 'x' and 'w' must have the same length. I know I can use summarise() and weighted.mean, but in this way I need to specify all the column names, which is not what I want.

result = df%>%
  group_by(A)%>%
  summarise_all(function(x){weighted.mean(x, .$B)})

Here the data frame has group column A, weight column B and other columns. I expect to have weighted averages of other columns values by column B for each group in A. I hope I can do this using dplyr and weighted.mean, but I am OK with other available methods.

Upvotes: 3

Views: 1649

Answers (1)

akrun
akrun

Reputation: 887251

We dont' need .$ as .$ extract the whole column value instead of the values that corresponds to the grouping structure

df %>%
   group_by(A)%>%
   summarise_all(list(~ weighted.mean(., B)))

It can also be written without a lambda function (~) if we provide the parameters explicitly

df %>%
   group_by(A)%>%
   summarise_all(weighted.mean, w = B)

Upvotes: 0

Related Questions