Re: [R] FUN argument to return a vector in aggregate function

2010-05-05 Thread David Winsemius
On May 5, 2010, at 5:32 PM, utkarshsinghal wrote: Extending my question further, I want to apply different FUN arguments on three fields and the "by" argument also contains more than one field. For example: set.seed(100) d = data.frame(a=sample(letters[1:2], 20 ,replace = T ),b = sam

Re: [R] FUN argument to return a vector in aggregate function

2010-05-05 Thread Gabor Grothendieck
Try this: do.call("rbind", by(d, d[1:2], function(x) with(x, data.frame(x[1, 1:2], `mean c` = mean(c), `sum d` = sum(d), `has X` = "X" %in% e, check.names = FALSE or this (which uses 1 or 0 to mean TRUE or FALSE in the last column): > library(sqldf) # see http://sqldf.googlecode.com > sqldf

Re: [R] FUN argument to return a vector in aggregate function

2010-05-05 Thread utkarshsinghal
Extending my question further, I want to apply different FUN arguments on three fields and the "by" argument also contains more than one field. For example: set.seed(100) d = data.frame(a=sample(letters[1:2],20,replace=T),b=sample(3,20,replace=T),c=rpois(20,1),d=rbinom(20,1,0.5),e=rep(c("X","Y")

Re: [R] FUN argument to return a vector in aggregate function

2009-11-23 Thread Gabor Grothendieck
Try this: > library(doBy) > summaryBy(breaks ~ ., warpbreaks, FUN = c(mean, sum, length)) wool tension breaks.mean breaks.sum breaks.length 1A L44.6401 9 2A M24.0216 9 3A H24.6221 9

Re: [R] FUN argument to return a vector in aggregate function

2009-11-23 Thread Jim Lemon
On 11/23/2009 07:15 PM, utkarshsinghal wrote: Hi All, I am currently doing the following to compute summary statistics of aggregated data: a = aggregate(warpbreaks$breaks, warpbreaks[,-1], mean) b = aggregate(warpbreaks$breaks, warpbreaks[,-1], sum) c = aggregate(warpbreaks$breaks, warpbreaks[

[R] FUN argument to return a vector in aggregate function

2009-11-23 Thread utkarshsinghal
Hi All, I am currently doing the following to compute summary statistics of aggregated data: a = aggregate(warpbreaks$breaks, warpbreaks[,-1], mean) b = aggregate(warpbreaks$breaks, warpbreaks[,-1], sum) c = aggregate(warpbreaks$breaks, warpbreaks[,-1], length) ans = cbind(a, b[,3], c[,3]) Thi