簡體   English   中英

R 用戶自定義/動態匯總 function 內 dplyr::summarise

[英]R user-defined/dynamic summary function within dplyr::summarise

如果聽起來沒有很多類似的問題,很難定義這個問題!

我有一個 function,我希望其中一個參數是 function 名稱,該名稱將傳遞給 dplyr::summarise,例如“mean”

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
  }

(這按 gear 和 cyl 分組,然后返回,每組,count 和 mean(disp))

根據我的筆記,我希望“意思”是動態的,執行 zFun 定義的 function,但我一生都無法弄清楚如何去做。 提前感謝您的任何建議。

您可以使用match.fun使 function 動態化。 我還刪除zColquo ,因為它不需要。

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)
}

然后您可以檢查 output

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 

我們可以使用get

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


  zColquo = quo_name(zCol)
  x %>%
  group_by(gear, !!sym(groupcol)) %>% # 1 preset grouper, 1 user-defined
  summarise(Count = n(), # 1 preset summary, 1 user defined
            !!zColquo := get(zFun)(!!sym(zCol))) %>% 
ungroup
 }

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 

此外,我們可以刪除group_bysummariseacross sym評估,如果我們用 cross

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



 x %>%
    group_by(across(c(gear, groupcol))) %>% # 1 preset grouper, 1 user-defined
    summarise(Count = n(), # 1 preset summary, 1 user defined
            across(zCol, ~ get(zFun)(.))) %>% 
    ungroup
 }
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 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM