[R] average among one factor in a nested dataframe

From: Junqian Gordon Xu <xjqian_at_gmail.com>
Date: Sun, 24 Apr 2011 23:42:24 -0500


I have two nested data frames:

a<-rnorm(6)
b<-rnorm(9)

f1<-c("x1","x2","x3"))
f2<-c("y1","y2")
id<-c(1:6)

a_df<-data.frame(cbind(id,f1,"y1",a))
id<-c(1:9)
b_df<-data.frame(cbind(id,f1,"y2",b))

I want to preserve id and f1, but want to collapse f2 and take the corresponding mean values of a and b. Missing value in either dataframe should be handled properly (i.e., just take the non-missing number without dividing by 2).

I had a look at rowSum/Means and s/l/tapply, but couldn't figure out how to handle this case cleanly. Any suggestions?

Thanks
Gordon



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. Received on Mon 25 Apr 2011 - 06:43:59 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 25 Apr 2011 - 11:40:34 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive