utkarshsinghal
2009-Nov-23 08:15 UTC
[R] FUN argument to return a vector in aggregate function
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]) This seems unnecessarily complex to me so I tried > aggregate(warpbreaks$breaks, warpbreaks[,-1], function(z) c(mean(z),sum(z),length(z))) but aggregate doesn't allow FUN argument to return a vector. I tried "by", "tapply" and several other functions as well but the output needed further modifications to get the same format as "ans" above. Is there any other function same as aggregate which allow FUN argument to return vector. Regards Utkarsh
Petr PIKAL
2009-Nov-23 08:59 UTC
[R] Odp: FUN argument to return a vector in aggregate function
Hi r-help-bounces at r-project.org napsal dne 23.11.2009 09:15:02:> 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]) > > This seems unnecessarily complex to me so I tried > > aggregate(warpbreaks$breaks, warpbreaks[,-1], function(z) > c(mean(z),sum(z),length(z))) > but aggregate doesn't allow FUN argument to return a vector. > > I tried "by", "tapply" and several other functions as well but the > output needed further modifications to get the same format as "ans"above. It is work for sapply/split. sapply(split(warpbreaks$breaks, warpbreaks[,-1]), function(x) c(mean(x), sum(x), length(x))) A.L B.L A.M B.M A.H B.H [1,] 44.55556 28.22222 24 28.77778 24.55556 18.77778 [2,] 401.00000 254.00000 216 259.00000 221.00000 169.00000 [3,] 9.00000 9.00000 9 9.00000 9.00000 9.00000>Regards Petr> > Is there any other function same as aggregate which allow FUN argument > to return vector. > > Regards > Utkarsh > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.
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[,-1], length) > ans = cbind(a, b[,3], c[,3]) > > This seems unnecessarily complex to me so I tried > > aggregate(warpbreaks$breaks, warpbreaks[,-1], function(z) > c(mean(z),sum(z),length(z))) > but aggregate doesn't allow FUN argument to return a vector. > > I tried "by", "tapply" and several other functions as well but the > output needed further modifications to get the same format as "ans" > above. > > Is there any other function same as aggregate which allow FUN argument > to return vector. >Hi Utkarsh, Does this do what you want? data(warpbreaks) library(prettyR) # can't use "length" as it doesn't have na.rm argument brkdn(breaks~wool+tension,warpbreaks, num.desc=c("mean","sum","valid.n")) Jim
Gabor Grothendieck
2009-Nov-23 13:14 UTC
[R] FUN argument to return a vector in aggregate function
Try this:> library(doBy) > summaryBy(breaks ~ ., warpbreaks, FUN = c(mean, sum, length))wool tension breaks.mean breaks.sum breaks.length 1 A L 44.55556 401 9 2 A M 24.00000 216 9 3 A H 24.55556 221 9 4 B L 28.22222 254 9 5 B M 28.77778 259 9 6 B H 18.77778 169 9 On Mon, Nov 23, 2009 at 3:15 AM, utkarshsinghal <utkarsh.singhal at global-analytics.com> 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[,-1], length) > ans = cbind(a, b[,3], c[,3]) > > This seems unnecessarily complex to me so I tried >> aggregate(warpbreaks$breaks, warpbreaks[,-1], function(z) >> c(mean(z),sum(z),length(z))) > but aggregate doesn't allow FUN argument to return a vector. > > I tried "by", "tapply" and several other functions as well but the output > needed further modifications to get the same format as "ans" above. > > Is there any other function same as aggregate which allow FUN argument to > return vector. > > Regards > Utkarsh > > ______________________________________________ > 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. >