Henrique, thanks for the response. As a new user, I was unaware of with() and ave(), those are both very helpful.
However, I don't think your method is quite right. I for all rows in each Key, I want the mean( IAC) where ProfA <= 0. To simplify the problem, I have made a subset dataframe that contains only the rows required for the mean (data frame names changed from the original email) ... FDP0D<-subset( FD, abs( FD$ProfA) < .01 & FD$IncA < 70, select=c(Key,IAC)) The first few rows of FDP0D look like this (note row names) ... > FDP0D Key IAC 11 B6-VB000.VB00045.3.12.3.0 0.889 12 B6-VB000.VB00045.3.12.3.0 0.889 13 B6-VB000.VB00045.3.12.3.0 0.890 14 B6-VB000.VB00045.3.12.3.0 0.890 15 B6-VB000.VB00045.3.12.3.0 0.891 16 B6-VB000.VB00045.3.12.3.0 0.892 17 B6-VB000.VB00045.3.12.3.0 0.893 83 B6-VB020.VB02045.3.12.3.0 0.852 84 B6-VB020.VB02045.3.12.3.0 0.852 85 B6-VB020.VB02045.3.12.3.0 0.852 (etc ... 630 rows) Now I do the ave( ) > X<-with( FDP0D, ave( IAC, list( Key), mean)) And I get ... function (x, ...) UseMethod("mean") <environment: namespace:base> Error in unique.default(x) : unique() applies only to vectors > What's up? unique() seems to work OK on Key ... > with( FDP0D, unique( Key)) [1] "B6-VB000.VB00045.3.12.3.0" "B6-VB020.VB02045.3.12.3.0" [3] "B6-VB040.VB04045.3.12.3.0" "B6-VB060.VB06045.3.12.3.0" [5] "B6-VB080.VB08045.3.12.3.0" "B6-VB100.VB10045.3.12.3.0" [7] "BC6-VB000.VB00045.3.12.3.0" "BC6-VB020.VB02045.3.12.3.0" [9] "BC6-VB040.VB04045.3.12.3.0" "BC6-VB060.VB06045.3.12.3.0" (etc, 90 values which is correct) Thanks, Chip Barnaby At 03:09 PM 4/30/2008, Henrique Dallazuanna wrote: >If I understand your question: > > x$IAC0 <- with(x, ave(IAC, list(ProfA, Key), FUN = mean)) > >On Wed, Apr 30, 2008 at 3:52 PM, Chip Barnaby ><<mailto:[EMAIL PROTECTED]>[EMAIL PROTECTED]> wrote: >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. > >Thanks! > >Chip Barnaby > > > > > > > > > >--------------------------------------------------------- >Chip >Barnaby ><mailto:[EMAIL PROTECTED]>[EMAIL PROTECTED] >Vice President of Research >Wrightsoft Corp. 781-862-8719 x118 voice >131 Hartwell Ave 781-861-2058 fax >Lexington, MA 02421 <http://www.wrightsoft.com>www.wrightsoft.com > >______________________________________________ ><mailto:R-help@r-project.org>R-help@r-project.org mailing list ><https://stat.ethz.ch/mailman/listinfo/r-help>https://stat.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide ><http://www.R-project.org/posting-guide.html>http://www.R-project.org/posting-guide.html >and provide commented, minimal, self-contained, reproducible code. > > > > >-- >Henrique Dallazuanna >Curitiba-Paraná-Brasil >25° 25' 40" S 49° 16' 22" O --------------------------------------------------------- Chip Barnaby [EMAIL PROTECTED] Vice President of Research Wrightsoft Corp. 781-862-8719 x118 voice 131 Hartwell Ave 781-861-2058 fax Lexington, MA 02421 www.wrightsoft.com --------------------------------------------------------- [[alternative HTML version deleted]]
______________________________________________ R-help@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.