使用data.table通过不同函数汇总列

时间:2017-01-26 14:14:45

标签: r data.table

假设df是一个包含许多列的数据表。我想按variable1进行分组,并为f中的所有变量汇总函数variables_f的数据表,并使用g汇总variables_g <中的所有变量/ p>

1 个答案:

答案 0 :(得分:1)

假设'variables_f'和'variables_g'是表示列名的vector个字符串character,我们按'variable1'分组,获取这些vector的值list mget lapply,循环显示f,应用函数gc,然后连接(df[, c(lapply(mget(variables_f), f), lapply(mget(variables_g), g)), by = variable1]

set.seed(24)
df <- data.frame(variable1 = rep(LETTERS[1:5], each = 2), 
           matrix(sample(1:9, 10*20, replace=TRUE), ncol=20,
          dimnames = list(NULL,  
 c(paste0('var_f', 1:10), paste0('var_g', 1:10)))))
setDT(df)
variables_f <- paste0("var_f", 1:10)
variables_g <- paste0("var_g", 1:10)

f <- function(x) mean(x)
g <- function(x) max(x) 

数据

deployer:
  image: jenkins
  volumes:
    - "/mnt/jenkins:/var/jenkins_home"
    - "/var/run/docker.sock:/var/run/docker.sock"
  ports:
    - "2375:2375"
    - "8080:8080"
    - "50000:50000"