Succinct way to summarize different columns with different functions

后端 未结 4 1291
既然无缘
既然无缘 2021-01-13 15:21

My question builds on a similar one by imposing an additional constraint that the name of each variable should appear only once.

Consider a data frame



        
4条回答
  •  醉梦人生
    2021-01-13 15:48

    Here's a hacky function that uses unexported functions from dplyr so it is not future proof, but you can specify a different summary for each column.

    summarise_with <- function(.tbl, .funs) {
      funs <- enquo(.funs)
      syms <- syms(tbl_vars(.tbl))
      calls <- dplyr:::as_fun_list(.funs, funs, caller_env())
      stopifnot(length(syms)==length(calls))
      cols <- purrr::map2(calls, syms, ~dplyr:::expr_substitute(.x, quote(.), .y))
      cols <- purrr::set_names(cols, purrr::map_chr(syms, rlang::as_string))
      summarize(.tbl, !!!cols)
    }
    

    Then you could do

    df %>% summarise_with(list(mean, sum))
    

    and not have to type the column names at all.

提交回复
热议问题