GerasimosPanagiotakopoulos GerasimosPanagiotakopoulos - 13 days ago 5x
R Question

Aggregation in data.table by reference to column names

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:

x =
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?


We can use c with names

x[, sum(Sepal.Width), by = c(names(x)[c(1, 5)])]