--I am getting the following error message when trying to use weighted_mean as the aggregation function for summarise_at:
Here is my code:
Error in summarise_impl(.data, dots) : Evaluation error: 'x' and 'w' must have the same length.
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.
To unsubscribe from this group and stop receiving emails from it, send an email to manipulatr+...@googlegroups.com.