[datatable-help] Using list valued columns with by
Chris Neff
caneff at gmail.com
Tue Feb 21 21:18:16 CET 2012
Hi all,
A colleague asked me a question, and while I found a solution it
doesn't seem quite optimal.
The example:
data <- data.table(x=rep(1:2,each=10), y=rnorm(20), key="x")
f <- function(y) {
return( list(a=mean(y), b=rep(y[1],10) )
}
result <- data[, list(f(y)), by=x]
What winds up happening is that result winds up having V1 alternate
between f(y)$a and f(y)$b, resulting in 4 rows, 2 for each value of x.
What I want instead is result to have 2 rows, with V1 being the list
that gets returned from f(y).
I have found that this works:
result <- data[, list(list(f(y))), by=x]
But then I have to do:
result[J(1),][,V1][[1]]
to get the same thing I would get from f(result[J(1),][,V1]). I want
to lose the [[1]] but I can't seem to see how I would do so. Really
what I would envision is like with sapply, I want to do
result <- data[, f(y), by=x, simplify=FALSE]
But of course simplify isn't an argument for data.table. Thoughts?
-Chris
More information about the datatable-help
mailing list