Does anyone have something like tapply for matrices? I would like to average the contents of columns according to factors but tapply works only with one column at each time. Is something available to do everything in one step? Many thanks. Manuel Montes
try this: mat <- matrix(rnorm(1000), 100, 10) ind <- gl(4, 25) rowsum(mat, ind) / rep(table(ind), each = ncol(mat)) I hope it helps. Best, Dimitris ---- Dimitris Rizopoulos Biostatistical Centre School of Public Health Catholic University of Leuven Address: Kapucijnenvoer 35, Leuven, Belgium Tel: +32/(0)16/336899 Fax: +32/(0)16/337015 Web: http://med.kuleuven.be/biostat/ http://www.student.kuleuven.be/~m0390867/dimitris.htm ----- Original Message ----- From: "Manuel Montes" <jmmontes at uni-hohenheim.de> To: <r-help at r-project.org> Sent: Tuesday, April 08, 2008 11:14 AM Subject: [R] tapply> Does anyone have something like tapply for matrices? I would like to > average the contents of columns > according to factors but tapply works only with one column at each > time. Is something available to do > everything in one step? > Many thanks. > Manuel Montes > > ______________________________________________ > 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. >Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm
On 4/8/2008 5:14 AM, Manuel Montes wrote:> Does anyone have something like tapply for matrices? I would like to average the contents of columns > according to factors but tapply works only with one column at each time. Is something available to do > everything in one step? > Many thanks. > Manuel MontesHow about aggregate()? For example: aggregate(iris[,c("Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width")], by = list(Species = iris$Species), FUN = mean) Species Sepal.Length Sepal.Width Petal.Length Petal.Width 1 setosa 5.006 3.428 1.462 0.246 2 versicolor 5.936 2.770 4.260 1.326 3 virginica 6.588 2.974 5.552 2.026> ______________________________________________ > 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.-- Chuck Cleland, Ph.D. NDRI, Inc. (www.ndri.org) 71 West 23rd Street, 8th floor New York, NY 10010 tel: (212) 845-4495 (Tu, Th) tel: (732) 512-0171 (M, W, F) fax: (917) 438-0894