Hi, R users, 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))But I got the error: Error in match.fun(FUN) : 'mean(y$a)' is not a function, character or symbol Anyone can tell me how to fix this problem? Thanks. Gary [[alternative HTML version deleted]]
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]]
Hi Gary, I'm not sure if you can do that with aggregate. You might have to resort to something like tmp <- cbind(aggregate(y$a, by=list(y$x), mean), aggregate(y$b, by=list(y$x), sum)$x) names(tmp) <- c("x", "mean.a", "sum.b") tmp Or, using the plyr package: library(plyr) ddply(y, .(x), summarize, mean.a=mean(a), sum.b=sum(b)) Best, Ista On Tue, Feb 22, 2011 at 5:25 PM, Hongwei Dong <pdxdong at gmail.com> wrote:> Hi, R users, > > 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)) > > But I got the error: > > Error in match.fun(FUN) : > ?'mean(y$a)' is not a function, character or symbol > > > Anyone can tell me how to fix this problem? Thanks. > > Gary > > ? ? ? ?[[alternative HTML version deleted]] > > ______________________________________________ > 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. >-- Ista Zahn Graduate student University of Rochester Department of Clinical and Social Psychology http://yourpsyche.org
try data.table: x a b 1 1 1 21 2 2 2 22 3 3 3 23 4 4 4 24 5 5 5 25 6 1 6 26 7 2 7 27 8 3 8 28 9 4 9 29 10 5 10 30 11 1 11 31 12 2 12 32 13 3 13 33 14 4 14 34 15 5 15 35> require(data.table)Loading required package: data.table Quick start guide : vignette("datatable-intro") Homepage : http://datatable.r-forge.r-project.org/> y <- data.table(y) > y[, list(mean = mean(a), sum=sum(b)), by = x]x mean sum [1,] 1 6 78 [2,] 2 7 81 [3,] 3 8 84 [4,] 4 9 87 [5,] 5 10 90 On Tue, Feb 22, 2011 at 5:25 PM, Hongwei Dong <pdxdong at gmail.com> wrote:> Hi, R users, > > 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)) > > But I got the error: > > Error in match.fun(FUN) : > ?'mean(y$a)' is not a function, character or symbol > > > Anyone can tell me how to fix this problem? Thanks. > > Gary > > ? ? ? ?[[alternative HTML version deleted]] > > ______________________________________________ > 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. >-- Jim Holtman Data Munger Guru What is the problem that you are trying to solve?
The documentation of aggregate tells you that your way will not work. Why don't you aggregate/sum the columns separately? I would doubt that what you want to try to achieve in one go is already implemented somewhere.... Jannis --- Hongwei Dong <pdxdong at gmail.com> schrieb am Di, 22.2.2011:> Von: Hongwei Dong <pdxdong at gmail.com> > Betreff: [R] "aggregate" in R > An: r-help at r-project.org > Datum: Dienstag, 22. Februar, 2011 22:25 Uhr > Hi, R users, > > 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)) > > But I got the error: > > Error in match.fun(FUN) : > ? 'mean(y$a)' is not a function, character or symbol > > > Anyone can tell me how to fix this problem? Thanks. > > Gary > > ??? [[alternative HTML version deleted]] > > ______________________________________________ > 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. >
Use ?plyr::ddply --------------------------------------------------------------------------- Jeff Newmiller The ..... ..... Go Live... DCN:<jdnewmil@dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/Batteries O.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k --------------------------------------------------------------------------- Sent from my phone. Please excuse my brevity. Hongwei Dong <pdxdong@gmail.com> wrote: Hi, R users, 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)) But I got the error: Error in match.fun(FUN) : 'mean(y$a)' is not a function, character or symbol Anyone can tell me how to fix this problem? Thanks. Gary [[alternative HTML version deleted]]_____________________________________________ R-help@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. [[alternative HTML version deleted]]