How to feed a list of unquoted column names into `lapply` (so that I can use it with a `dplyr` function)

前端 未结 3 1804
陌清茗
陌清茗 2021-02-08 14:33

I am trying to write a function in tidyverse/dplyr that I want to eventually use with lapply (or map). (I had been working on it to answer

3条回答
  •  夕颜
    夕颜 (楼主)
    2021-02-08 15:05

    I'm not really a dplyr afficionado, but for what its worth here is how you could achieve this using library(data.table) instead:

    setDT(sample_data)
    
    gen_report <- function(report_cat){
      sample_data[ , .(num = .N, total = sum(AMOUNT), REPORT_CATEGORY = report_cat), 
                   by = .(REPORT_VALUE = get(report_cat), YEAR)] 
    }
    
    gen_report('REPORT_CODE')
    lapply(cat.list, gen_report)
    

提交回复
热议问题