Try this: aggregate(example[c('x', 'y')], example[c('id', 'Year')], 'mean')
On Mon, Aug 9, 2010 at 11:46 AM, steven mosher <mosherste...@gmail.com>wrote: > Assuming a data frame or matrix with two columns representing variable that > you want to aggregate over. > you want to calculate column means, by year, for each Id > > > > > example<-data.frame(id=c(rep(12345,5),rep(54321,6),rep(45678,7)),Year=rep(seq(1900,1902,by=1),6), > x=seq(1,18,by=1),y=seq(18,1,by=-1)) > example > id Year x y > 1 12345 1900 1 18 > 2 12345 1901 2 17 > 3 12345 1902 3 16 > 4 12345 1900 4 15 > 5 12345 1901 5 14 > 6 54321 1902 6 13 > 7 54321 1900 7 12 > 8 54321 1901 8 11 > 9 54321 1902 9 10 > 10 54321 1900 10 9 > 11 54321 1901 11 8 > 12 45678 1902 12 7 > 13 45678 1900 13 6 > 14 45678 1901 14 5 > 15 45678 1902 15 4 > 16 45678 1900 16 3 > 17 45678 1901 17 2 > 18 45678 1902 18 1 > > result<-by(example[,3:4], example$id, by(example[,3:4], > example$Year,colMeans, na.rm=T)) > Error in FUN(X[[1L]], ...) : could not find function "FUN > > > desired result should look like: > id Year meanx mean y > 1 12345 1900 ... ... > 2 12345 1901 ... > 3 12345 1902 ... > 4 54321 1900 > 5 54321 1901 > 6 54321 1902 > 7 45678 1900 > 8 45678 1901 > 9 45678 1902 > > [[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. > -- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[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.