简体   繁体   中英

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?

One option is setNames

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM