Skip to content
Prev 265541 / 398503 Next

cbind in aggregate formula - based on an existing object (vector)

You may find it easier to use the data.frame method for aggregate
instead of the formula method when you are using vectors of column
names.   E.g.,

  responseVars <- c("mpg", "wt")
  byVars <- c("cyl", "gear")
  aggregate(mtcars[responseVars], by=mtcars[byVars], FUN=median)

gives the same result as

  aggregate(cbind(mpg, wt) ~ cyl + gear, FUN=median, data=mtcars)

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com 

-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Dimitri Liakhovitski
Sent: Thursday, July 14, 2011 1:45 PM
To: David Winsemius
Cc: r-help
Subject: Re: [R] cbind in aggregate formula - based on an existing object (vector)

Thanks a lot!

actually, what I tried to do is very simple - just passing tons of
variable names into the formula. Maybe that "get" thing suggested by
Bert would work...

Dimitri
On Thu, Jul 14, 2011 at 4:01 PM, David Winsemius <dwinsemius at comcast.net> wrote: