What is the idiomatic data.table method for creating a data table with separate columns for the elements of a vector returned by a function computed by a group?
Consider the data table:
library(data.table) data(iris) setDT(iris)
If the function is range() , I need a result similar to:
iris[, .(min_petal_width = min(Petal.Width), max_petal_width = max(Petal.Width) ), keyby = Species]
but using the range() function.
I can use dcast , but this is ugly:
dcast( iris[, .( petal_width = range(Petal.Width), value = c("min_petal_width", "max_petal_width")), keyby = Species], Species ~ value, value.var = "petal_width")
I hope there will be a simpler expression:
iris[, (c("min_petal_width","max_petal_width")) = range(Petal.Width), keyby = Species]
source share