How to set names for by argument in data.table?

Viewed 108

I don't want to use setNames function, but do it in the statement:

iris2 <- data.table(iris)
iris2[,.(value = mean(Sepal.Length)), by = "Species"]

For example i want have names: value and group.

PS: this is crazy! Using agregate cost 1200 ms. Doint the same with data.table cost now 30ms.!!! (on another dataset, my own big one)

1 Answers

Just like we place the j inside the .(), the by arguments can be placed like that and named

iris2[, .(value = mean(Sepal.Length)), by = .(group = Species)] 
Related