dplyr :: summarise中的R用户定义/动态汇总函数 [英] R user-defined/dynamic summary function within dplyr::summarise

查看:67
本文介绍了dplyr :: summarise中的R用户定义/动态汇总函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在不听起来像很多类似问题的情况下,很难定义这个问题!

Somewhat hard to define this question without sounding like lots of similar questions!

我有一个函数,我希望其中一个参数成为函数名,该函数名将传递给dplyr :: summarise,例如平均"或"sum":

I have a function for which I want one of the parameters to be a function name, that will be passed to dplyr::summarise, e.g. "mean" or "sum":

data(mtcars)
  f <- function(x = mtcars,
                groupcol = "cyl",
                zCol = "disp",
                zFun = "mean") {
    
    zColquo = quo_name(zCol)
    
    cellSummaries <- x %>%
      group_by(gear, !!sym(groupcol)) %>% # 1 preset grouper, 1 user-defined
      summarise(Count = n(), # 1 preset summary, 1 user defined
                !!zColquo := mean(!!sym(zColquo))) # mean should be zFun, user-defined
    ungroup
  }

(按齿轮和圆柱体分组,然后每组返回计数和均值(显示))

(this groups by gear and cyl, then returns, per group, count and mean(disp))

根据我的笔记,我希望'mean'是动态的,执行zFun定义的功能,但是我一生无法解决如何做到这一点!预先感谢您的任何建议.

Per my note, I'd like 'mean' to be dynamic, performing the function defined by zFun, but I can't for the life of me work out how to do it! Thanks in advance for any advice.

推荐答案

您可以使用 match.fun 使该函数动态化.我还删除了 zColquo ,因为它不是必需的.

You can use match.fun to make the function dynamic. I also removed zColquo as it's not needed.

library(dplyr)
library(rlang)

f <- function(x = mtcars,
              groupcol = "cyl",
              zCol = "disp",
              zFun = "mean") {

  cellSummaries <- x %>%
                   group_by(gear, !!sym(groupcol)) %>% 
                   summarise(Count = n(), 
                             !!zCol := match.fun(zFun)(!!sym(zCol))) %>%
                   ungroup

  return(cellSummaries)
}

然后您可以检查输出

f()

# A tibble: 8 x 4
#   gear   cyl Count  disp
#  <dbl> <dbl> <int> <dbl>
#1     3     4     1  120.
#2     3     6     2  242.
#3     3     8    12  358.
#4     4     4     8  103.
#5     4     6     4  164.
#6     5     4     2  108.
#7     5     6     1  145 
#8     5     8     2  326 

f(zFun = "sum")

# A tibble: 8 x 4
#   gear   cyl Count  disp
#  <dbl> <dbl> <int> <dbl>
#1     3     4     1  120.
#2     3     6     2  483 
#3     3     8    12 4291.
#4     4     4     8  821 
#5     4     6     4  655.
#6     5     4     2  215.
#7     5     6     1  145 
#8     5     8     2  652 

这篇关于dplyr :: summarise中的R用户定义/动态汇总函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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