使用其他列作为在summary_at()中起作用的参数 [英] use of other columns as arguments to function in summarize_at()

查看:31
本文介绍了使用其他列作为在summary_at()中起作用的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这很棒:

> 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参数.可悲的是,明显的尝试失败了:

But now I want to use one of the columns from mtcars as the w argument to weighted.mean. Sadly, the obvious attempt fails:

> mtcars %>% group_by(cyl) %>% summarize_at(vars(disp, hp), weighted.mean, w = wt)
Error in dots_list(...) : object 'wt' not found

即使wt实际上确实是mtcar的一部分.我如何使用其他列作为summary_at()中函数的参数?

Even though wt is, indeed, part of mtcars. How can I use other columns as arguments to a function within summarize_at()?

推荐答案

您可以尝试 funs()语法:

mtcars %>% group_by(cyl) %>% 
        summarize_at(vars(disp, hp), funs(weighted.mean(.,wt)))
#    cyl  disp    hp
#  <dbl> <dbl> <dbl>
#1  4.00   110  83.4
#2  6.00   185 122  
#3  8.00   362 209  

这篇关于使用其他列作为在summary_at()中起作用的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆