Zhaochen He
Zhaochen He

Reputation: 660

aggregate data programmatically in data.table

I'd like to be able to use data.table in a loop to aggregate some data. The following code works:

mydata = data.table(a = rnorm(10), b = rnorm(10))
sum = list()
sum$a = mydata[, list(sum_a = sum(a))]
sum$b = mydata[, list(sum_b = sum(b))]

However, the code below fails:

sumlist = list()
for (var in c('a','b')) {
   sumlist[[var]] = mydata[, list(paste0('sum_', var) = sum(get(var)))]
}

I know the problem is with the contents of the call to list() inside the data.table brackets, but I'm not sure how to specify it. Most of the existing questions pertain to the assignment of new variables with ":=", for which a version of the above code does work. Please advise.

Upvotes: 1

Views: 45

Answers (1)

akrun
akrun

Reputation: 887851

Naming on the lhs of = wouldn't work. We can use setNames

sumlist = list()
for (var in c('a','b')) {
    sumlist[[var]] = mydata[, setNames(list(sum(get(var))), paste0('sum_', var))]
 }

-output

sumlist
#$a
#       sum_a
#1: 0.0328273

#$b
#         sum_b
#1: -0.04686505

Upvotes: 1

Related Questions