[R] variation of the aggregate function

Gabor Grothendieck ggrothendieck at gmail.com
Fri Aug 13 19:02:24 CEST 2010


On Fri, Aug 13, 2010 at 12:26 PM, Eva Nordstrom <eva.nordstrom at yahoo.com> wrote:
> Is there a "more efficient/elegant" way to obtain the result "z" below.
>
> a <- c('pink','pink','blue','blue','gold','gold')
> b <- c(5,8,9,12,7,4)
> agg <- aggregate(x=b,by=list(a), FUN='mean')
> m <- match(a, agg[,1])
> z <- agg[m,2]
> z
>

Try:

   ave(b, a)



More information about the R-help mailing list