Hi, From time to time I need to do the aggregation. To illustrate, I present a toy example as below. In this example, the task is to aggregate x and y by z with the function mean. Could I call the aggregation function with x_test, where x_test=c("x","y")? Thanks Miao> dftest<-data.frame(x=1:12, y=(1:12)%%4, z=(1:12)%%2) > dftestx y z 1 1 1 1 2 2 2 0 3 3 3 1 4 4 0 0 5 5 1 1 6 6 2 0 7 7 3 1 8 8 0 0 9 9 1 1 10 10 2 0 11 11 3 1 12 12 0 0> aggregate(cbind(x,y)~z, data=dftest, FUN=mean)z x y 1 0 7 1 2 1 6 2> x_test=c("x","y") > aggregate(cbind(x_test)~z, data=dftest, FUN=mean)Error in model.frame.default(formula = cbind(x_test) ~ z, data = dftest) : variable lengths differ (found for 'z') a1aggregate(cbind(factor(x_test))~z, data=dftest, FUN=mean) Error in model.frame.default(formula = cbind(factor(x_test)) ~ z, data dftest) : variable lengths differ (found for 'z')> aggregate(factor(x_test)~z, data=dftest, FUN=mean)Error in model.frame.default(formula = factor(x_test) ~ z, data = dftest) : variable lengths differ (found for 'z') [[alternative HTML version deleted]]
?with(dftest,aggregate(cbind(x,y),list(z),FUN=mean)) #? Group.1 x y #1?????? 0 7 1 #2?????? 1 6 2 #or library(plyr) ddply(dftest,.(z),numcolwise(mean)) #? z x y #1 0 7 1 #2 1 6 2 A.K. ----- Original Message ----- From: jpm miao <miaojpm at gmail.com> To: r-help <r-help at r-project.org> Cc: Sent: Thursday, May 23, 2013 3:05 AM Subject: [R] convert a character string to a name Hi, ? From time to time I need to do the aggregation. To illustrate, I present a toy example as below. In this example, the task is to aggregate x and y by z with the function mean. ? Could I call the aggregation function with x_test, where ? x_test=c("x","y")? Thanks Miao> dftest<-data.frame(x=1:12, y=(1:12)%%4, z=(1:12)%%2) > dftest? ? x y z 1? 1 1 1 2? 2 2 0 3? 3 3 1 4? 4 0 0 5? 5 1 1 6? 6 2 0 7? 7 3 1 8? 8 0 0 9? 9 1 1 10 10 2 0 11 11 3 1 12 12 0 0> aggregate(cbind(x,y)~z, data=dftest, FUN=mean)? z x y 1 0 7 1 2 1 6 2> x_test=c("x","y") > aggregate(cbind(x_test)~z, data=dftest, FUN=mean)Error in model.frame.default(formula = cbind(x_test) ~ z, data = dftest) : ? variable lengths differ (found for 'z') a1aggregate(cbind(factor(x_test))~z, data=dftest, FUN=mean) Error in model.frame.default(formula = cbind(factor(x_test)) ~ z, data dftest) : ? variable lengths differ (found for 'z')> aggregate(factor(x_test)~z, data=dftest, FUN=mean)Error in model.frame.default(formula = factor(x_test) ~ z, data = dftest) : ? variable lengths differ (found for 'z') ??? [[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.
try this:> dftest<-data.frame(x=1:12, y=(1:12)%%4, z=(1:12)%%2) > aggregate(cbind(x,y)~z, data=dftest, FUN=mean)z x y 1 0 7 1 2 1 6 2> x_test=c("x","y") > a <- formula(paste0('cbind('+ , x_test[1] + , ',' + , x_test[2] + , ') ~ z' + ))> acbind(x, y) ~ z> aggregate(a, data = dftest, FUN = mean)z x y 1 0 7 1 2 1 6 2>On Thu, May 23, 2013 at 3:05 AM, jpm miao <miaojpm@gmail.com> wrote:> Hi, > From time to time I need to do the aggregation. To illustrate, I present > a toy example as below. In this example, the task is to aggregate x and y > by z with the function mean. > Could I call the aggregation function with x_test, where > x_test=c("x","y")? Thanks > > Miao > > > > dftest<-data.frame(x=1:12, y=(1:12)%%4, z=(1:12)%%2) > > dftest > x y z > 1 1 1 1 > 2 2 2 0 > 3 3 3 1 > 4 4 0 0 > 5 5 1 1 > 6 6 2 0 > 7 7 3 1 > 8 8 0 0 > 9 9 1 1 > 10 10 2 0 > 11 11 3 1 > 12 12 0 0 > > aggregate(cbind(x,y)~z, data=dftest, FUN=mean) > z x y > 1 0 7 1 > 2 1 6 2 > > x_test=c("x","y") > > aggregate(cbind(x_test)~z, data=dftest, FUN=mean) > Error in model.frame.default(formula = cbind(x_test) ~ z, data = dftest) : > variable lengths differ (found for 'z') > a1aggregate(cbind(factor(x_test))~z, data=dftest, FUN=mean) > Error in model.frame.default(formula = cbind(factor(x_test)) ~ z, data > dftest) : > variable lengths differ (found for 'z') > > aggregate(factor(x_test)~z, data=dftest, FUN=mean) > Error in model.frame.default(formula = factor(x_test) ~ z, data = dftest) : > variable lengths differ (found for 'z') > > [[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. >-- Jim Holtman Data Munger Guru What is the problem that you are trying to solve? Tell me what you want to do, not how you want to do it. [[alternative HTML version deleted]]
Here are a couple of approaches:> dftest<-data.frame(x=1:12, y=(1:12)%%4, z=(1:12)%%2) > x_test=c("x","y") > aggregate( dftest[,x_test], dftest['z'], FUN=mean )z x y 1 0 7 1 2 1 6 2> > ### Or > > tmp.f <- as.formula( paste( 'cbind(',+ paste( x_test, collapse=',' ), + ') ~ z' ) )> aggregate( tmp.f, data=dftest, FUN=mean )z x y 1 0 7 1 2 1 6 2 The first just uses x_test to subset the data frame and sends the constructed subset to aggregate. The second constructs the formula from the strings and passes the formula to aggregate. On Thu, May 23, 2013 at 1:05 AM, jpm miao <miaojpm@gmail.com> wrote:> Hi, > From time to time I need to do the aggregation. To illustrate, I present > a toy example as below. In this example, the task is to aggregate x and y > by z with the function mean. > Could I call the aggregation function with x_test, where > x_test=c("x","y")? Thanks > > Miao > > > > dftest<-data.frame(x=1:12, y=(1:12)%%4, z=(1:12)%%2) > > dftest > x y z > 1 1 1 1 > 2 2 2 0 > 3 3 3 1 > 4 4 0 0 > 5 5 1 1 > 6 6 2 0 > 7 7 3 1 > 8 8 0 0 > 9 9 1 1 > 10 10 2 0 > 11 11 3 1 > 12 12 0 0 > > aggregate(cbind(x,y)~z, data=dftest, FUN=mean) > z x y > 1 0 7 1 > 2 1 6 2 > > x_test=c("x","y") > > aggregate(cbind(x_test)~z, data=dftest, FUN=mean) > Error in model.frame.default(formula = cbind(x_test) ~ z, data = dftest) : > variable lengths differ (found for 'z') > a1aggregate(cbind(factor(x_test))~z, data=dftest, FUN=mean) > Error in model.frame.default(formula = cbind(factor(x_test)) ~ z, data > dftest) : > variable lengths differ (found for 'z') > > aggregate(factor(x_test)~z, data=dftest, FUN=mean) > Error in model.frame.default(formula = factor(x_test) ~ z, data = dftest) : > variable lengths differ (found for 'z') > > [[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. >-- Gregory (Greg) L. Snow Ph.D. 538280@gmail.com [[alternative HTML version deleted]]