变量作为data.table聚合列表中的名称

I'm aggregating an R/data.table (v1.12.2) and I need to use a variable as the name of the aggregated column. E.g.:

library(data.table)

DT <- data.table(x= 1:5, y= c('A', 'A', 'B', 'B', 'B'))

aggname <- 'max_x'  ## 'max_x' should be the name of the aggregated column

DT2 <- DT[, list(aggname= max(x)), by= y]
DT2
   y aggname  <- This should be 'max_x' not 'aggname'!
1: A       2
2: B       5

之后,我可以使用以下方式重命名该列:

setnames(DT2, 'aggname', aggname)
DT2
   y max_x
1: A     2
2: B     5

但是我必须检查字符串'aggname'不会首先创建重复的名称。有什么更好的方法吗?

评论
终淡化了美
终淡化了美

We can use setNames on the list column

DT[, setNames(.(max(x)), aggname), by = y]
#    y max_x
#1: A     2
#2: B     5

aggname2 <- 'min_x'
DT[, setNames(.(max(x), min(x)), c(aggname, aggname2)), by = y]
#   y max_x min_x
#1: A     2     1
#2: B     5     3

Or another option is lst from dplyr

library(dplyr)
DT[, lst(!! aggname := max(x)), by = y]
#    y max_x
#1: A     2
#2: B     5


DT[, lst(!! aggname := max(x), !! aggname2 := min(x)), by = y]
#   y max_x min_x
#1: A     2     1
#2: B     5     3
点赞
评论