【发布时间】:2018-03-20 22:16:17
【问题描述】:
这很好用:
> mtcars %>% group_by(cyl) %>% summarize_at(vars(disp, hp), weighted.mean)
# A tibble: 3 x 3
cyl disp hp
<dbl> <dbl> <dbl>
1 4.00 105 82.6
2 6.00 183 122
3 8.00 353 209
但现在我想使用 mtcars 中的一列作为 weighted.mean 的 w 参数。可悲的是,明显的尝试失败了:
> mtcars %>% group_by(cyl) %>% summarize_at(vars(disp, hp), weighted.mean, w = wt)
Error in dots_list(...) : object 'wt' not found
尽管 wt 确实是 mtcars 的一部分。如何使用其他列作为 summarise_at() 中函数的参数?
【问题讨论】: