[R] cbind in aggregate formula - based on an existing object (vector)
dwinsemius at comcast.net
Thu Jul 14 22:01:24 CEST 2011
as.matrix makes a matrix out of the dataframe that is passed to it.
As a further note I attempted and failed for reasons that are unclear
to me to construct a formula that would (I hoped) preserve the column
names which are being mangle in the posted effort:
form <- as.formula(paste(
paste( myvars, collapse=","),
") ~ group+mydate",
sep=" ") )
> example.agg1<-aggregate(formula=form,data=example, FUN=sum)
Error in m[[2L]][[2L]] : object of type 'symbol' is not subsettable
2: aggregate.formula(formula = form, data = example, FUN = sum)
1: aggregate(formula = form, data = example, FUN = sum)
cbind(value1, value2) ~ group + mydate
cbind(value1, value2), group + mydate)
So it seems to be correctly dispatched to aggregate.formula but not
passing some check or another. Also tried with formula() rather than
as.formula with identical error message. Also tried including without
naming the argument.
On Jul 14, 2011, at 3:32 PM, Dimitri Liakhovitski wrote:
> Thank you, David, it does work.
> Could you please explain why? What exactly does changing it to "as
> matrix" do?
> Thank you!
> On Thu, Jul 14, 2011 at 3:25 PM, David Winsemius <dwinsemius at comcast.net
> > wrote:
>> On Jul 14, 2011, at 3:05 PM, Dimitri Liakhovitski wrote:
>>> I am aggregating using a formula in aggregate - of the type:
>>> However, I actually have an object (vector of my variables to be
>>> I'd like my aggregate formula (its "cbind" part) to be able to use
>>> "myvars" object. Is it possible?
>>> Thanks for your help!
>> Not sure I have gotten all the way there, but this does work:
>> group mydate example[myvars] NA
>> 1 group1 2008-12-01 4 4.2
>> 2 group2 2008-12-01 6 6.2
>> 3 group1 2009-01-01 40 40.2
>> 4 group2 2009-01-01 60 60.2
>> 5 group1 2009-02-01 400 400.2
>> 6 group2 2009-02-01 600 600.2
>>> Reproducible example:
>>> mydate = rep(seq(as.Date("2008-12-01"), length = 3, by = "month"),4)
>>> # this works
>>> ### Building my object (vector of 2 names - in reality, many more):
>>> ### does not work
>>> Dimitri Liakhovitski
>>> Ninah Consulting
>>> R-help at r-project.org mailing list
>>> PLEASE do read the posting guide
>>> and provide commented, minimal, self-contained, reproducible code.
>> David Winsemius, MD
>> West Hartford, CT
> Dimitri Liakhovitski
> Ninah Consulting
David Winsemius, MD
West Hartford, CT
More information about the R-help