jimdare
2009-Nov-06 01:53 UTC
[R] Calculate Mean for each Treatment/Trial Interaction in DF
Hi, I am create a new DF that summarizes the mean angle per treatment/trial, of the original DF (see below). I have had some success using: (tapply(df$Angle, INDEX=interaction(df$State, df$Trial), FUN=mean)); however, this gives the answer as a list, which means I would have to split the name to get the categories back. Does anyone know a simple way to transform the Original DF into Summary DF? Thanks in advance, James Original DF> dfAngle Trial Treatment 1 43.1297 1 C 2 62.3613 1 C 3 88.2767 2 C 4 75.2431 2 C 5 91.3668 3 C 6 61.2800 3 C 7 55.5575 1 U 8 69.4661 1 U 9 67.5512 2 U 10 95.5528 2 U 11 75.8689 3 U 12 66.9070 3 U Summary DF> sdfAngle Trial Treatment 1 52.7455 1 C 2 ... 2 C 3 ... 3 C 4 ... 1 U 5 ... 2 U 6 ... 3 U -- View this message in context: http://old.nabble.com/Calculate-Mean-for-each-Treatment-Trial-Interaction-in-DF-tp26225533p26225533.html Sent from the R help mailing list archive at Nabble.com.
Peter Alspach
2009-Nov-06 02:35 UTC
[R] Calculate Mean for each Treatment/Trial Interaction in DF
Tena koe Jim ?aggregate HTH .... Peter Alspach> -----Original Message----- > From: r-help-bounces at r-project.org > [mailto:r-help-bounces at r-project.org] On Behalf Of jimdare > Sent: Friday, 6 November 2009 2:54 p.m. > To: r-help at r-project.org > Subject: [R] Calculate Mean for each Treatment/Trial Interaction in DF > > > Hi, > > I am create a new DF that summarizes the mean angle per > treatment/trial, of the original DF (see below). I have had > some success using: > (tapply(df$Angle, INDEX=interaction(df$State, df$Trial), > FUN=mean)); however, this gives the answer as a list, which > means I would have to split the name to get the categories back. Does > anyone know a simple way to transform the Original DF into > Summary DF? > > Thanks in advance, > > James > > > Original DF > > > df > Angle Trial Treatment > 1 43.1297 1 C > 2 62.3613 1 C > 3 88.2767 2 C > 4 75.2431 2 C > 5 91.3668 3 C > 6 61.2800 3 C > 7 55.5575 1 U > 8 69.4661 1 U > 9 67.5512 2 U > 10 95.5528 2 U > 11 75.8689 3 U > 12 66.9070 3 U > > Summary DF > > > sdf > > Angle Trial Treatment > 1 52.7455 1 C > 2 ... 2 C > 3 ... 3 C > 4 ... 1 U > 5 ... 2 U > 6 ... 3 U > > -- > View this message in context: > http://old.nabble.com/Calculate-Mean-for-each-Treatment-Trial- > Interaction-in-DF-tp26225533p26225533.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. >
David Winsemius
2009-Nov-06 03:03 UTC
[R] Calculate Mean for each Treatment/Trial Interaction in DF
On Nov 5, 2009, at 7:53 PM, jimdare wrote:> > I am create a new DF that summarizes the mean angle per treatment/ > trial, of > the original DF (see below). I have had some success using: > (tapply(df$Angle, INDEX=interaction(df$State, > df$Trial), FUN=mean));That's rather difficult to accept, since State is not a column name of df.> however, this gives the answer as a list, which > means I would have to split the name to get the categories back. Does > anyone know a simple way to transform the Original DF into Summary DF? > > Thanks in advance, > > James > > > Original DF > >> df > Angle Trial Treatment > 1 43.1297 1 C > 2 62.3613 1 C > 3 88.2767 2 C > 4 75.2431 2 C > 5 91.3668 3 C > 6 61.2800 3 C > 7 55.5575 1 U > 8 69.4661 1 U > 9 67.5512 2 U > 10 95.5528 2 U > 11 75.8689 3 U > 12 66.9070 3 U > > Summary DF > >> sdf > > Angle Trial Treatment > 1 52.7455 1 C > 2 ... 2 C> snip >Try: aggregate(df$Angle, list(df$Treatment, df$Trial), sum) Group.1 Group.2 x 1 C 1 105.4910 2 U 1 125.0236 3 C 2 163.5198 4 U 2 163.1040 5 C 3 152.6468 6 U 3 142.7759> --David Winsemius, MD Heritage Laboratories West Hartford, CT
Jorge Ivan Velez
2009-Nov-06 03:06 UTC
[R] Calculate Mean for each Treatment/Trial Interaction in DF
Hi Jim, Here is a suggestion using aggregate(): res <- with(df, aggregate(Angle, list(Trial, Treatment), mean, na.rm TRUE)) colnames(res) <- c('Trial','Treatment', 'Average_Angle') res # Trial Treatment Average_Angle # 1 1 C 52.7455 # 2 2 C 81.7599 # 3 3 C 76.3234 # 4 1 U 62.5118 # 5 2 U 81.5520 HTH, Jorge On Thu, Nov 5, 2009 at 8:53 PM, jimdare <> wrote:> > Hi, > > I am create a new DF that summarizes the mean angle per treatment/trial, of > the original DF (see below). I have had some success using: > (tapply(df$Angle, INDEX=interaction(df$State, > df$Trial), FUN=mean)); however, this gives the answer as a list, which > means I would have to split the name to get the categories back. Does > anyone know a simple way to transform the Original DF into Summary DF? > > Thanks in advance, > > James > > > Original DF > > > df > Angle Trial Treatment > 1 43.1297 1 C > 2 62.3613 1 C > 3 88.2767 2 C > 4 75.2431 2 C > 5 91.3668 3 C > 6 61.2800 3 C > 7 55.5575 1 U > 8 69.4661 1 U > 9 67.5512 2 U > 10 95.5528 2 U > 11 75.8689 3 U > 12 66.9070 3 U > > Summary DF > > > sdf > > Angle Trial Treatment > 1 52.7455 1 C > 2 ... 2 C > 3 ... 3 C > 4 ... 1 U > 5 ... 2 U > 6 ... 3 U > > -- > View this message in context: > http://old.nabble.com/Calculate-Mean-for-each-Treatment-Trial-Interaction-in-DF-tp26225533p26225533.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]]