1

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 Answers1

2

We can use c with names

x[, sum(Sepal.Width), by = c(names(x)[c(1, 5)])] 
djhurio
  • 5,437
  • 4
  • 27
  • 48
akrun
  • 874,273
  • 37
  • 540
  • 662