Hi folk, I tried this and it works just perfectly tapply(iris[,1],iris[5],mean) but, how to obtain a single table from multiple variables? In tapply x is an atomic object so this code doesn't work tapply(iris[,1:4],iris[5],mean) Thanx and great summer holidays Gianandrea -- View this message in context: http://www.nabble.com/multiple-tapply-tp18868063p18868063.html Sent from the R help mailing list archive at Nabble.com.
Try this: aggregate(iris[,1:4], list(Species = iris[,5]), mean) On Thu, Aug 7, 2008 at 8:01 AM, glaporta <glaporta at freeweb.org> wrote:> > Hi folk, > I tried this and it works just perfectly > tapply(iris[,1],iris[5],mean) > but, how to obtain a single table from multiple variables? > In tapply x is an atomic object so this code doesn't work > tapply(iris[,1:4],iris[5],mean) > > Thanx and great summer holidays > Gianandrea > -- > View this message in context: http://www.nabble.com/multiple-tapply-tp18868063p18868063.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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. >-- Henrique Dallazuanna Curitiba-Paran?-Brasil 25? 25' 40" S 49? 16' 22" O
one way would be: mapply(tapply, iris[,1:4], MoreArgs=list(iris[,5], mean)) K On Thu, Aug 7, 2008 at 2:01 PM, glaporta <glaporta@freeweb.org> wrote:> > Hi folk, > I tried this and it works just perfectly > tapply(iris[,1],iris[5],mean) > but, how to obtain a single table from multiple variables? > In tapply x is an atomic object so this code doesn't work > tapply(iris[,1:4],iris[5],mean) > > Thanx and great summer holidays > Gianandrea > -- > View this message in context: > http://www.nabble.com/multiple-tapply-tp18868063p18868063.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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. >[[alternative HTML version deleted]]
On 8/7/2008 7:01 AM, glaporta wrote:> Hi folk, > I tried this and it works just perfectly > tapply(iris[,1],iris[5],mean) > but, how to obtain a single table from multiple variables? > In tapply x is an atomic object so this code doesn't work > tapply(iris[,1:4],iris[5],mean) > > Thanx and great summer holidays > GianandreaHere is one way: > apply(iris[,1:4], 2, function(x){tapply(x, list(iris[,5]), mean)}) Sepal.Length Sepal.Width Petal.Length Petal.Width setosa 5.006 3.428 1.462 0.246 versicolor 5.936 2.770 4.260 1.326 virginica 6.588 2.974 5.552 2.026 Here is another: > library(reshape) > iris.melt <- melt(iris, measure.var=names(iris)[1:4]) > cast(iris.melt, variable + Species ~ ., mean) variable Species (all) 1 Sepal.Length setosa 5.006 2 Sepal.Length versicolor 5.936 3 Sepal.Length virginica 6.588 4 Sepal.Width setosa 3.428 5 Sepal.Width versicolor 2.770 6 Sepal.Width virginica 2.974 7 Petal.Length setosa 1.462 8 Petal.Length versicolor 4.260 9 Petal.Length virginica 5.552 10 Petal.Width setosa 0.246 11 Petal.Width versicolor 1.326 12 Petal.Width virginica 2.026 -- 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
On 8/7/2008 7:01 AM, glaporta wrote:> Hi folk, > I tried this and it works just perfectly > tapply(iris[,1],iris[5],mean) > but, how to obtain a single table from multiple variables? > In tapply x is an atomic object so this code doesn't work > tapply(iris[,1:4],iris[5],mean) > > Thanx and great summer holidays > GianandreaAnd a third approach: > sapply(split(iris[,1:4], iris$Species), colMeans) setosa versicolor virginica Sepal.Length 5.006 5.936 6.588 Sepal.Width 3.428 2.770 2.974 Petal.Length 1.462 4.260 5.552 Petal.Width 0.246 1.326 2.026 -- 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
Here are three ways: # 1 aggregate(iris[-5], iris[5], mean) # 2 library(doBy) summaryBy(.~Species, iris, keep = TRUE) # 3 library(sqldf) sqldf("select Species, avg(Sepal_Length) Sepal_Length, avg(Sepal_Width) Sepal_Width, avg(Petal_Length) Petal_Length, avg(Petal_Width) Petal_Width from iris group by Species") On Thu, Aug 7, 2008 at 7:01 AM, glaporta <glaporta at freeweb.org> wrote:> > Hi folk, > I tried this and it works just perfectly > tapply(iris[,1],iris[5],mean) > but, how to obtain a single table from multiple variables? > In tapply x is an atomic object so this code doesn't work > tapply(iris[,1:4],iris[5],mean) > > Thanx and great summer holidays > Gianandrea > -- > View this message in context: http://www.nabble.com/multiple-tapply-tp18868063p18868063.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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. >