[R] aggregate.formula: formula from string
arun
smartpink111 at yahoo.com
Wed Oct 31 13:43:29 CET 2012
Hi Thorn,
May be it is a bug in aggregate.formula(). Not sure about it.
The solutions that I gave and yours were returning the colnames as NA.
I guess this should be much better in terms of getting the result in one step.
do.call(aggregate,list(mF,d,mean))
# a b y z
#1 1 A -1.48354978 -0.37141485
#2 2 A -0.08862713 0.35359250
#3 3 A 1.17519518 -0.47595290
#4 1 B 0.10214686 -0.70005131
#5 2 B 0.41185154 0.03707291
#6 3 B 0.20507062 -0.67946389
A.K.
----- Original Message -----
From: "Thaler,Thorn,LAUSANNE,Applied Mathematics" <Thorn.Thaler at rdls.nestle.com>
To: arun <smartpink111 at yahoo.com>
Cc: R help <r-help at r-project.org>
Sent: Wednesday, October 31, 2012 8:30 AM
Subject: RE: [R] aggregate.formula: formula from string
Dear Arun,
Thanks for your suggestion, that does the trick. Just because I'm curious, where does the problem come from? I figured that wrapping the formula object in brackets would work as well:
aggregate((mF), d, mean)
So I guess it has something to do with the scope of mF, or what is the root cause?
Thanks for your help!
KR,
-Thorn
> -----Original Message-----
> From: arun [mailto:smartpink111 at yahoo.com]
> Sent: Mittwoch, 31. Oktober 2012 13:15
> To: Thaler,Thorn,LAUSANNE,Applied Mathematics
> Cc: R help
> Subject: Re: [R] aggregate.formula: formula from string
>
> Hi,
> Try this:
> res<- aggregate(eval(mF),d,mean)
>
> res
>
> # a b NA NA
> #1 1 A -1.48354978 -0.37141485
> #2 2 A -0.08862713 0.35359250
> #3 3 A 1.17519518 -0.47595290
> #4 1 B 0.10214686 -0.70005131
> #5 2 B 0.41185154 0.03707291
> #6 3 B 0.20507062 -0.67946389
>
>
> res1<-aggregate(cbind(y, z) ~ a + b, d, mean)
> colnames(res)[3:4]<-colnames(res1)[3:4]
> identical(res,res1)
> #[1] TRUE
> A.K.
>
>
>
>
> ----- Original Message -----
> From: "Thaler,Thorn,LAUSANNE,Applied Mathematics"
> <Thorn.Thaler at rdls.nestle.com>
> To: "r-help at r-project.org" <r-help at r-project.org>
> Cc:
> Sent: Wednesday, October 31, 2012 5:46 AM
> Subject: [R] aggregate.formula: formula from string
>
> Dear all,
>
> I want to use aggregate.formula to conveniently summarize a data.frame. I have
> quiet some variables in the data.frame and thus I don't want to write all
> these names by hand, but instead create them on the fly. This approach has the
> advantage that if there will be even more columns in the data.frame I don't
> have to change the code.
>
> I've hence tried to construct a formula object and to pass that to aggregate:
>
> d <- expand.grid(a = factor(1:3), b = factor(LETTERS[1:2]))
> d <- rbind(d,d,d)
> d$y <- rnorm(18)
> d$z <- rnorm(18)
> mF <- as.formula(paste("cbind(", paste(names(d)[-(1:2)], collapse = ","), ") ~
> a + b", sep = ""))
>
> But if I try to pass that formula to aggregate
>
> aggregate(mF, d, mean)
>
> I get the following error:
>
> Error in m[[2L]][[2L]] : object of type 'symbol' is not subsettable
>
> But if I pass the formula directly:
>
> aggregate(cbind(y, z) ~ a + b, d, mean)
>
> Everything is working as expected.
>
> So I was wondering what went wrong? I know I could use a formula like . ~ a +
> b instead and this would work fine, but I'm just interested in why the
> outlined approach does not work as expected, and where my mistake lies? (that
> means in particular I am not asking for a solution of how to get the thing
> done - there are plenty of alternatives - but instead to understand why this
> very approach does not work)
>
> Thanks for your help!
>
> Kind Regards,
>
> Thorn Thaler
> Mathematician
>
> Applied Mathematics
> Nestec Ltd,
> Nestlé Research Center
> PO Box 44
> CH-1000 Lausanne 26
> Phone: +41 21 785 8220
> Fax: +41 21 785 9486
>
> ______________________________________________
> 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.
>
More information about the R-help
mailing list