1

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

I can rename the column(s) afterwards with something like:

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

But I would have to check that the string 'aggname' doesn't create duplicate names first. Is there any better way of doing it?

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks - How can I use setNames with more than one column? LIke DT[, list(aggname= max(x), aggname2= min(x)), by = y]
Nevermind - I think I got it: DT[, setNames(c(.(max(x)), .(min(x))), c(aggname, aggname2)), by = y] - Thanks again I didn't know about setNames I'm still figuring out the menaing of that leading dot.
@dariober i did update the post. The .( is just a concise way for list(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.