Changing names of resulting variables in custom dplyr function

前端 未结 1 914
我寻月下人不归
我寻月下人不归 2021-01-20 02:42

Background

In order to speed up generating grouped summaries across multiple tables; as I\'m doing most of that while in dplyr workflow, I\'ve drafted a simple fun

1条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-20 03:42

    Maybe use rename:

    library(tidyverse)
    
    generate_summary_tbl <- function(dataset, group_column, summary_column, useColname = FALSE) {
        group_column   <- enquo(group_column)
        summary_column <- enquo(summary_column)
        dataset %>% 
            group_by(!!group_column) %>% 
            summarise(
                mean = mean(!!summary_column),
                sum  = sum(!!summary_column)
                # Other metrics that need to be generated frequently
            ) %>% 
            ungroup -> smryDta
    
        if (useColname) 
          smryDta <- smryDta %>%  
          rename_at(
            vars(-one_of(quo_name(group_column))), 
            ~paste(quo_name(group_column), .x, sep="_")
          )
    
        return(smryDta)
    }
    
    mtcars %>% generate_summary_tbl(am, mpg)
    # # A tibble: 2 x 3
    #      am     mean   sum
    #        
    # 1     0 17.14737 325.8
    # 2     1 24.39231 317.1
    mtcars %>% generate_summary_tbl(am, mpg, T)
    #   # A tibble: 2 x 3
    #      am  am_mean am_sum
    #         
    # 1     0 17.14737  325.8
    # 2     1 24.39231  317.1
    

    0 讨论(0)
提交回复
热议问题