[R] Aggregate() questions

Chip Barnaby cbarnaby at wrightsoft.com
Wed Apr 30 20:52:18 CEST 2008

Dear all --

I have a data frame containing data related to heat gain through 
windows.  The general form is ...

Key  ProfA    IAC   <many other numeric columns>
AAA        0    .7
AAA      10    .6
AAA        0   .66
AAA      20   .45
(more AAA rows)
(then AAB rows)

'Key' identifies the physical configuration ... rows with a given Key 
contain data for same window under various conditions.

I want to add a column IAC0 containing, for each Key, the mean IAC of 
all rows with ProfA == 0.

The general approach I think I need is approximately (the following 
is not known to run) --

1) XS<-subset( X, ProfA < .01) to get the ProfA == 0 rows.  This 
could be done implicitly as part of step 2, I suppose.

2) X0<-aggregate( XS[,"IAC"], by=list( Key=XS$Key), mean)

3) XF<-merge( X, X0, by="Key")

4) Change new col name to "IAC0" (see Question 2).

Question 1: Is this a reasonable way to solve my problem?

Question 2: The aggregate() result has 2 columns: "Key" and 
"x".   Can the name of the mean column be specified (short of 
renaming after the fact)?  All the aggregate() examples (e.g. in 
help()) elegantly produce the "right result" with nicely named 
columns etc.  I can't seem to make things work so smoothly.


Chip Barnaby

Chip Barnaby                   cbarnaby at wrightsoft.com
Vice President of Research
Wrightsoft Corp.               781-862-8719 x118 voice
131 Hartwell Ave               781-861-2058 fax
Lexington, MA 02421         www.wrightsoft.com

More information about the R-help mailing list