I tried to write a fuction with data.table and here's an example about the idea:
d <- data.frame(x=1:6, y=6:1, z= rep(c('A', 'B', 'C'), 2))
func <- function(q){
o <- setDT(d)[, list(maxi = max(q)), by = z]
}
func(x)
the returned error said object x can't be found. I looked at this and this, tried do.call and data.frame selection, with no good result:
o <- setDT(d)[, maxi = do.call('max', list(q)), by = z]
o <- setDT(d)[, maxi = do.call('max', list(d$q)), by = z]
o <- setDT(d)[, list(maxi = max(d[,q])), by = z]
it returned "unused argument", "object not found", or "invalid 'type' (closure) of argument". Am I following the previous answers wrong or data.table needs some special tuning? To see if this only happens with data.table, I also tested o <- aggregate(d[, q] ~ z, d, max)
and the result was no good either. Thanks for any help!