[R] tapply

Chuck Cleland ccleland at optonline.net
Tue Apr 8 13:13:18 CEST 2008


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 Montes

   How 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



More information about the R-help mailing list