问题
I would like to aggregate some columns by a list of columns in a data.table. However, I would like to refrain from using the column names outside the quotation marks (in the by = .(desiredColumn1, desiredColumn2), that is). I am happy with using either the column names or the column indices. For example:
library(data.table)
x = as.data.table(iris)
x[, sum(Sepal.Width), by = .(Sepal.Length, Species)] # I want to avoid doing this
x[, sum("Sepal.Width"), by = .("Sepal.Length", "Species"), with = FALSE] # this does not work
x[, sum("Sepal.Width"), by = .(1, 5), with = FALSE]
Any ideas on how to do this?
回答1:
We can use c with names
x[, sum(Sepal.Width), by = c(names(x)[c(1, 5)])]
来源:https://stackoverflow.com/questions/39954534/aggregation-in-data-table-by-reference-to-column-names