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

Jim Lemon jim at bitwrit.com.au
Mon Nov 23 10:02:46 CET 2009


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




More information about the R-help mailing list