> > 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")
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 could easily be > done? > > Thanks in advance. > > [[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. ______________________________________________ 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.