Junqian Gordon Xu
2011-Apr-25 04:42 UTC
[R] average among one factor in a nested dataframe
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
Kenneth Roy Cabrera Torres
2011-Apr-25 11:14 UTC
[R] average among one factor in a nested dataframe
Hi Junquian: I try your code (there is a typo, I believe) 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)) But I don't understand the "nested" databases. I see that both have f1 variable but I do not see f2 variable in any of them. So, what do you mean with "collapse f2"? Maybe you need to first "merge()" de databases and then "aggregate()" them. Have a nice day! El dom, 24-04-2011 a las 23:42 -0500, Junqian Gordon Xu escribi?:> 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.
Apparently Analagous Threads
- code rows depending on the value of other rows in multilevel dataframe
- Lattice and horizontally stacked density plots
- regex expression to select row or column
- weighing proportion of rowSums in dataframe
- passing an argument to a function which is also to be a dataframe column name