Hi all, I have a dataframe that includes data on individuals that are distributed across multiple rows. I have aggregated the data using ddply, but I have columns in the original data frame that are factors ( such as sites "A", "B", and "C") that I would like to include in the new data frame. I have done this in a clunky way using match() and a loop, but am wondering if there is a more elegant approach. Here is an example data set. #Example a<-c(rep(1:5,6)); b<-sort(b) b<-c(rep("A",10),rep("B",10),rep("C",10)) a<-c(rep(1:5,6)); b<-sort(b) d<-c(2008,2008,2009,2009,2010,2010);d<-rep(d,5) e<-rnorm(30,2,1) df<-data.frame(a,b,d,e) ; names(df)<-c("ind","site","year","height") I created a new factor using ind and year, and would basically like to include site in the new dataframe. Does anyone know how this could easily be done? Thanks in advance. [[alternative HTML version deleted]]
I have read it three times and still no concrete idea what you are actually trying to do, mainly because there is no information as to which level/variable you are aggregating on. It'd help if you provided the aggregated data (or sample rows thereof) so that we know what you want the result to be. Best, Daniel Wade Wall wrote:> > Hi all, > > I have a dataframe that includes data on individuals that are distributed > across multiple rows. I have aggregated the data using ddply, but I have > columns in the original data frame that are factors ( such as sites "A", > "B", and "C") that I would like to include in the new data frame. I have > done this in a clunky way using match() and a loop, but am wondering if > there is a more elegant approach. Here is an example data set. > > #Example > a<-c(rep(1:5,6)); b<-sort(b) > b<-c(rep("A",10),rep("B",10),rep("C",10)) > a<-c(rep(1:5,6)); b<-sort(b) > d<-c(2008,2008,2009,2009,2010,2010);d<-rep(d,5) > e<-rnorm(30,2,1) > df<-data.frame(a,b,d,e) ; names(df)<-c("ind","site","year","height") > > I created a new factor using ind and year, and would basically like to > include site in the new dataframe. Does anyone know how this could easily > be > done? > > Thanks in advance. > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- View this message in context: r.789695.n4.nabble.com/function-to-include-factors-in-summary-data-frame-tp3808391p3808538.html Sent from the R help mailing list archive at Nabble.com.
> > Hi all, > > I have a dataframe that includes data on individuals that aredistributed> across multiple rows. I have aggregated the data using ddply, but Ihave> columns in the original data frame that are factors ( such as sites "A", > "B", and "C") that I would like to include in the new data frame. Ihave> done this in a clunky way using match() and a loop, but am wondering if > there is a more elegant approach. Here is an example data set. > > #Example > a<-c(rep(1:5,6)); b<-sort(b) > b<-c(rep("A",10),rep("B",10),rep("C",10)) > a<-c(rep(1:5,6)); b<-sort(b) > d<-c(2008,2008,2009,2009,2010,2010);d<-rep(d,5) > e<-rnorm(30,2,1) > df<-data.frame(a,b,d,e) ; names(df)<-c("ind","site","year","height")Do you want something like this? with(df, aggregate(height, list(ind=ind, year=year, site=site), mean)) or ddply(df, .(ind,year,site), summarise, mean(height)) Regards Petr> > I created a new factor using ind and year, and would basically like to > include site in the new dataframe. Does anyone know how this couldeasily be> done? > > Thanks in advance. > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guideR-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.