Gary/Hongwei writes:
> I'm wondering how I can aggregate data in R with different functions for
> different columns. For example:
>
> x<-rep(1:5,3)
> y<-cbind(x,a=1:15,b=21:35)
> y<-data.frame(y)
>
> I want to aggregate "a" and "b" in y by "x". With "a", I want to use
> function "mean"; with "b", I want to use function "sum". I tried:
>
> > aggregate(y,x,mean(y$a),sum(y$b))
aggregate() only works with one function at a time, ?aggregate will
probably help.
This might be close to what you want.
y <- data.frame(x=rep(1:5,3), a=1:15, b=21:35)
answers <- merge(aggregate(list(abar=y$a), list(x=y$x), mean)
,aggregate(list(bbar=y$b), list(x=y$x), mean)
,by='x'
)
[[alternative HTML version deleted]]
______________________________________________
[email protected] 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.