[R] Repeated Aggregation with data.table

David Winsemius dwinsemius at comcast.net
Wed Aug 8 15:17:06 CEST 2012


On Aug 7, 2012, at 9:28 PM, arun wrote:

> HI,
>
> Try this:
>
> fun1<-function(x,.expr){
>   .expr<-expression(list(mean.z=mean(z),sd.z=sd(z)))
>  z1<-eval(.expr)
>  }
>
> #or
> fun1<-function(x,.expr){
>   .expr<-expression(list(mean.z=mean(z),sd.z=sd(z)))
>  z1<-.expr
>  }
>
>
>  dat[,eval(z1),list(x)]
> dat[,eval(z1),list(y)]
> dat[,eval(z1),list(x,y)]
>

I'm not seeing the connection between those functions and the  
data.table call. (Running that code produces an error on my machine.)  
If the goal is to have an expression result then just create it with  
expression(). In the example:

 > flist <- expression( list(mean.z = mean(z), sd.z = sd(z)) )
 > dat[ , eval(flist), list(x)]
    x      mean.z     sd.z
1: 2  0.04436034 1.039615
2: 3 -0.06354504 1.077686
3: 1 -0.08879671 1.066916

-- 
David.

> A.K.
>
>
>
> ----- Original Message -----
> From: Elliot Joel Bernstein <elliot.bernstein at fdopartners.com>
> To: r-help at r-project.org
> Cc:
> Sent: Tuesday, August 7, 2012 5:36 PM
> Subject: [R] Repeated Aggregation with data.table
>
> I have been using ddply to do aggregation, and I frequently define a
> single aggregation function that I use to aggregate over different
> groups. For example,
>
> require(plyr)
>
> dat <- data.frame(x = sample(3, 100, replace=TRUE), y = sample(3, 100,
> replace = TRUE), z = rnorm(100))
>
> f <- function(x) { data.frame(mean.z = mean(x$z), sd.z = sd(x$z)) }
>
> ddply(dat, "x", f)
> ddply(dat, "y", f)
> ddply(dat, c("x", "y"), f)
>
> I recently discovered the data.table package, which dramatically
> speeds up the aggregation:
>
> require(data.table)
> dat <- data.table(dat)
>
> dat[, list(mean.z = mean(z), sd.z = sd(z)), list(x)]
> dat[, list(mean.z = mean(z), sd.z = sd(z)), list(y)]
> dat[, list(mean.z = mean(z), sd.z = sd(z)), list(x,y)]
>
> But I can't figure out how to save the aggregation function
> "list(mean.z = mean(z), sd.z = sd(z))" as a variable that I can reuse,
> similar to the function "f" above. Can someone please explain how to
> do that?
>
> Thanks.
>
> - Elliot
>
> -- 
> Elliot Joel Bernstein, Ph.D. | Research Associate | FDO Partners, LLC
> 134 Mount Auburn Street | Cambridge, MA | 02138
> Phone: (617) 503-4619 | Email: elliot.bernstein at fdopartners.com
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

David Winsemius, MD
Alameda, CA, USA



More information about the R-help mailing list