R Question

Naming resulting data.table columns after aggregation in R

I want to make aggregations in a data.table using functions that return can multiple rows, like

summary()
or
quantile()
. The way I do it so far is this:

library(data.table)
x = as.data.table(iris)
x[, as.list(c(summary(Sepal.Length), summary(Sepal.Width))), by = Species]


This works, but results in arbitrary column names. If I have a vector of length 12,
myColumnNames
, how can I set the new columns to be named after this vector (without using setnames in the next row)?

My "obvious" attempt,
x[, myColumnNames = as.list(c(summary(Sepal.Length), summary(Sepal.Width))), by = Species]
failed. Any ideas?

Answer

One option is setNames

x[, setNames(as.list(c(summary(Sepal.Length), 
        summary(Sepal.Width))), myColumnNames), by = Species]