[R] cbind in aggregate formula - based on an existing object (vector)
gunter.berton at gene.com
Thu Jul 14 21:58:16 CEST 2011
Look at my vars from myvars<-c("value1","value2")
It's just a character vector of length 2!
You can't cbind a character vector of length 2! These are not
It's not at all clear to me what you ultimately want to do, but IF
it's: pass a character vector of names to be used as the LHS of the
aggregate .formula call, then something like: (untested)
MyVars <- do.call(cbind, lapply(myvars,get))
aggregate(MyVars ~ ...)
might do. But there are so many potential scoping problems here that I
would not be surprised if it failed. The usual advice for this sort of
thing is to use substitute() or maybe the dreaded eval(parse(...))
construction -- but as I said, I don't really understand what you're
On Thu, Jul 14, 2011 at 12:32 PM, Dimitri Liakhovitski
<dimitri.liakhovitski at gmail.com> 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 my
>>> "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
> R-help at r-project.org mailing list
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
"Men by nature long to get on to the ultimate truths, and will often
be impatient with elementary studies or fight shy of them. If it were
possible to reach the ultimate truths without the elementary studies
usually prefixed to them, these would not be preparatory studies but
-- Maimonides (1135-1204)
Genentech Nonclinical Biostatistics
More information about the R-help