I want to create a function used for data.table. Supposed we have
library(data.table)
DT = data.table(x=rep(c("a","b","c"),each=3), y=c(1,3,6), v=1:9)
foo <- function(data, field, grp){
data[, field, by=grp]
}
I have tried
foo(DT, .N, grp = y)
or
foo(DT, y)
They return errors. How do I pass input arguments in the data.table?
data.table:::`[.data.table`(DT, j=.N, by=y). I doubt that you need to use it like this, though. What does it gain you to usefoorather than the[]s?by. I am wondering if we could create function to help it.