Re: Error in using weighted_mean with summarise_at

96 views
Skip to first unread message

Brandon Hurr

unread,
Feb 20, 2018, 5:55:43 PM2/20/18
to Steven Barkin, manipulatr
Yeah, I'm not sure how to do it this way either, nor why it's throwing the error, but...

If you rearrange your data you can get to the same endpoint without too much trouble.

b %>%
    gather(key, value, -snapshot_date, -balance) %>% #gather up the variables you want to summarise
    group_by(key) %>% # the calculations will be done for each of the variables
    summarise(wm = weighted.mean(value, balance, na.rm=FALSE)) #take the weighted mean using the column names

# A tibble: 2 x 2
  key        wm
  <chr>   <dbl>
1 newrate 0.470
2 rate    0.305

If you want to do this with more columns, just make sure they are part of the dataset before the gather step. If you don't want to use some columns, just select against them or don't include them in the gather with a `-`.

HTH,
B

On Tue, Feb 20, 2018 at 8:18 AM, Steven Barkin <sbar...@gmail.com> wrote:
 

I am getting the following error message when trying to use weighted_mean as the aggregation function for summarise_at:


Error in summarise_impl(.data, dots) : Evaluation error: 'x' and 'w' must have the same length.



Here is my code:

a = data.frame(snapshot_date = c("1-Jan-2016", "1-Jan-2016", "1-Feb-2016", "1-Feb-2016"),
 rate
= c(0.75, 0.2, 0.5, 0.1), newrate = c(0.9, 0.4, 0.6, 0.3), balance = c(1000,2000,3000,4000))

b
= a %>% group_by(snapshot_date)

summarise_at
(.tbl = b, .vars = c("rate", "newrate"), .funs = weighted.mean, w = b$balance, na.rm = FALSE)



When I remove the weight argument, I no longer get an error message and I get correct (unweighted) results, as follows:

summarise_at(.tbl = b, .vars = c("rate", "newrate"), .funs = weighted.mean, na.rm = FALSE)



# A tibble: 2 x 3 snapshot_date rate newrate <fct> <dbl> <dbl> 1 1-Feb-2016 0.300 0.450 2 1-Jan-2016 0.475 0.650 >



What I am doing wrong when trying to use the weights?

--
You received this message because you are subscribed to the Google Groups "manipulatr" group.
To unsubscribe from this group and stop receiving emails from it, send an email to manipulatr+unsubscribe@googlegroups.com.
To post to this group, send email to manip...@googlegroups.com.
Visit this group at https://groups.google.com/group/manipulatr.
For more options, visit https://groups.google.com/d/optout.

mwh...@gmail.com

unread,
Mar 15, 2018, 3:48:51 PM3/15/18
to manipulatr
You can fix this using quo()

library(dplyr)
vars <- c("mpg", "drat")
mtcars %>% group_by(vs) %>% summarize_at(vars, weighted.mean, w = wt)     # Does NOT work
mtcars %>% group_by(vs) %>% summarize_at(vars, weighted.mean, w = ~ wt)     # Does NOT work
mtcars %>% group_by(vs) %>% summarize_at(vars, weighted.mean, w = .$wt)     # Does NOT work

mtcars %>% group_by(vs) %>% summarize_at(vars, weighted.mean, w = quo(wt))     # DOES work

To unsubscribe from this group and stop receiving emails from it, send an email to manipulatr+...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages