[R] aggregate.formula: formula from string

arun smartpink111 at yahoo.com
Wed Oct 31 13:15:29 CET 2012


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