Hi there: I have a question about generating mean value of a data.frame. Take iris data for example, if I have a data.frame looking like the following: --------------------- Sepal.Length Sepal.Width Petal.Length Petal.Width Species 1 5.1 3.5 1.4 0.2 setosa 2 4.9 3.0 1.4 0.2 setosa 3 4.7 3.2 1.3 0.2 setosa . . . . . . . . . . . . . . . . . . ----------------------- There are three different species in this table. I want to make a table and calculate mean value for each specie as the following table: ----------------- Sepal.Length Sepal.Width Petal.Length Petal.Width mean.setosa 5.006 3.428 1.462 0.246 mean.versicolor 5.936 2.770 4.260 1.326 mean.virginica 6.588 2.974 5.552 2.026 ----------------- Is there any short syntax can do it?? I mean shorter than the code I wrote as following: attach(iris) mean.setosa<-mean(iris[Species=="setosa", 1:4]) mean.versicolor<-mean(iris[Species=="versicolor", 1:4]) mean.virginica<-mean(iris[Species=="virginica", 1:4]) data.mean<-rbind(mean.setosa, mean.versicolor, mean.virginica) detach(iris) ------------------ Thanks a million!!! -- ====================================Shih-Hsiung, Chou System Administrator / PH.D Student at Department of Industrial Manufacturing and Systems Engineering Kansas State University [[alternative HTML version deleted]]
apply(iris[, -5], 2, tapply, iris$Species, mean) On Wed, Jun 9, 2010 at 3:43 PM, SH.Chou <cls3415@gmail.com> wrote:> Hi there: > I have a question about generating mean value of a data.frame. Take > iris data for example, if I have a data.frame looking like the following: > --------------------- > Sepal.Length Sepal.Width Petal.Length Petal.Width Species > 1 5.1 3.5 1.4 > 0.2 setosa > 2 4.9 3.0 1.4 > 0.2 setosa > 3 4.7 3.2 1.3 > 0.2 setosa > . . . . > . . > . . . . > . . > . . . . > . . > ----------------------- > There are three different species in this table. I want to make a table and > calculate mean value for each specie as the following table: > > ----------------- > Sepal.Length Sepal.Width Petal.Length > Petal.Width > mean.setosa 5.006 3.428 1.462 > 0.246 > mean.versicolor 5.936 2.770 4.260 > 1.326 > mean.virginica 6.588 2.974 5.552 > 2.026 > ----------------- > Is there any short syntax can do it?? I mean shorter than the code I wrote > as following: > > attach(iris) > mean.setosa<-mean(iris[Species=="setosa", 1:4]) > mean.versicolor<-mean(iris[Species=="versicolor", 1:4]) > mean.virginica<-mean(iris[Species=="virginica", 1:4]) > data.mean<-rbind(mean.setosa, mean.versicolor, mean.virginica) > detach(iris) > ------------------ > > Thanks a million!!! > > > -- > ====================================> Shih-Hsiung, Chou > System Administrator / PH.D Student at > Department of Industrial Manufacturing > and Systems Engineering > Kansas State University > > [[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. >[[alternative HTML version deleted]]
Here is an alternative with(iris, rowsum(iris[, -5], Species)/table(Species)) -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Peter Langfelder Sent: Thursday, 10 June 2010 12:27 PM To: SH.Chou Cc: r-help at r-project.org Subject: Re: [R] question about "mean" apply(iris[, -5], 2, tapply, iris$Species, mean) On Wed, Jun 9, 2010 at 3:43 PM, SH.Chou <cls3415 at gmail.com> wrote:> Hi there: > I have a question about generating mean value of a data.frame. Take > iris data for example, if I have a data.frame looking like the following: > --------------------- > Sepal.Length Sepal.Width Petal.Length Petal.Width Species > 1 5.1 3.5 1.4 > 0.2 setosa > 2 4.9 3.0 1.4 > 0.2 setosa > 3 4.7 3.2 1.3 > 0.2 setosa > . . . . > . . > . . . . > . . > . . . . > . . > ----------------------- > There are three different species in this table. I want to make a table and > calculate mean value for each specie as the following table: > > ----------------- > Sepal.Length Sepal.Width Petal.Length > Petal.Width > mean.setosa 5.006 3.428 1.462 > 0.246 > mean.versicolor 5.936 2.770 4.260 > 1.326 > mean.virginica 6.588 2.974 5.552 > 2.026 > ----------------- > Is there any short syntax can do it?? I mean shorter than the code I wrote > as following: > > attach(iris) > mean.setosa<-mean(iris[Species=="setosa", 1:4]) > mean.versicolor<-mean(iris[Species=="versicolor", 1:4]) > mean.virginica<-mean(iris[Species=="virginica", 1:4]) > data.mean<-rbind(mean.setosa, mean.versicolor, mean.virginica) > detach(iris) > ------------------ > > Thanks a million!!! > > > -- > ====================================> Shih-Hsiung, Chou > System Administrator / PH.D Student at > Department of Industrial Manufacturing > and Systems Engineering > Kansas State University > > [[alternative HTML version deleted]] > > ______________________________________________ > 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. >[[alternative HTML version deleted]] ______________________________________________ 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.
One possibility is> aggregate(iris[,-5],list(iris[,5]),mean)Group.1 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 - Phil Spector Statistical Computing Facility Department of Statistics UC Berkeley spector at stat.berkeley.edu On Wed, 9 Jun 2010, SH.Chou wrote:> Hi there: > I have a question about generating mean value of a data.frame. Take > iris data for example, if I have a data.frame looking like the following: > --------------------- > Sepal.Length Sepal.Width Petal.Length Petal.Width Species > 1 5.1 3.5 1.4 > 0.2 setosa > 2 4.9 3.0 1.4 > 0.2 setosa > 3 4.7 3.2 1.3 > 0.2 setosa > . . . . > . . > . . . . > . . > . . . . > . . > ----------------------- > There are three different species in this table. I want to make a table and > calculate mean value for each specie as the following table: > > ----------------- > Sepal.Length Sepal.Width Petal.Length > Petal.Width > mean.setosa 5.006 3.428 1.462 > 0.246 > mean.versicolor 5.936 2.770 4.260 > 1.326 > mean.virginica 6.588 2.974 5.552 > 2.026 > ----------------- > Is there any short syntax can do it?? I mean shorter than the code I wrote > as following: > > attach(iris) > mean.setosa<-mean(iris[Species=="setosa", 1:4]) > mean.versicolor<-mean(iris[Species=="versicolor", 1:4]) > mean.virginica<-mean(iris[Species=="virginica", 1:4]) > data.mean<-rbind(mean.setosa, mean.versicolor, mean.virginica) > detach(iris) > ------------------ > > Thanks a million!!! > > > -- > ====================================> Shih-Hsiung, Chou > System Administrator / PH.D Student at > Department of Industrial Manufacturing > and Systems Engineering > Kansas State University > > [[alternative HTML version deleted]] > > ______________________________________________ > 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. >
Hi split/sapply can be used besides other options sapply(split(iris[,1:4], iris$Species), mean) Regards Petr r-help-bounces at r-project.org napsal dne 10.06.2010 00:43:29:> Hi there: > I have a question about generating mean value of a data.frame. Take > iris data for example, if I have a data.frame looking like thefollowing:> --------------------- > Sepal.Length Sepal.Width Petal.Length Petal.Width Species > 1 5.1 3.5 1.4 > 0.2 setosa > 2 4.9 3.0 1.4 > 0.2 setosa > 3 4.7 3.2 1.3 > 0.2 setosa > . . . . > . . > . . . . > . . > . . . . > . . > ----------------------- > There are three different species in this table. I want to make a tableand> calculate mean value for each specie as the following table: > > ----------------- > Sepal.Length Sepal.Width Petal.Length > Petal.Width > mean.setosa 5.006 3.428 1.462 > 0.246 > mean.versicolor 5.936 2.770 4.260 > 1.326 > mean.virginica 6.588 2.974 5.552 > 2.026 > ----------------- > Is there any short syntax can do it?? I mean shorter than the code Iwrote> as following: > > attach(iris) > mean.setosa<-mean(iris[Species=="setosa", 1:4]) > mean.versicolor<-mean(iris[Species=="versicolor", 1:4]) > mean.virginica<-mean(iris[Species=="virginica", 1:4]) > data.mean<-rbind(mean.setosa, mean.versicolor, mean.virginica) > detach(iris) > ------------------ > > Thanks a million!!! > > > -- > ====================================> Shih-Hsiung, Chou > System Administrator / PH.D Student at > Department of Industrial Manufacturing > and Systems Engineering > Kansas State University > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.