tornanddesperate
2011-May-07 18:24 UTC
[R] how to calculate the mean of a group in a table
Hi its me again I don't mean to get on your nerves, but the use of R proofs to be a bit more complicated than envisaged. I would like to calculate the mean of a group of values, here "wage_accepted". The group is determined by the stage and period, so in the end there should be a column with the values of the wages in period 1 stage1, period 1 stage 2, period 2 stage1... Unfortunately, I haven't much of a clue on how to program this. Could you tell me how the function should roughly look like ? if-else, loops included or not ? in the end ? treatment session period stage wage_accepted type 1 1 1 1 1 25 low 2 1 1 1 1 19 low 3 1 1 1 1 15 low 4 1 1 1 2 32 high 5 1 1 1 2 13 low 6 1 1 1 2 14 low 7 1 1 2 1 17 low 8 1 1 2 1 12 low I am also very grateful if someone could point out a good internet tutorial resource for R functions such as if-else and loops and how to use them in conjunction with tables. It should contain simple examples if possible. As ever indepted to you, Matthias -- View this message in context: http://r.789695.n4.nabble.com/how-to-calculate-the-mean-of-a-group-in-a-table-tp3505986p3505986.html Sent from the R help mailing list archive at Nabble.com.
tornanddesperate wrote:> > Hi its me again > > I don't mean to get on your nerves, but the use of R proofs to be a bit > more complicated than envisaged. > > I would like to calculate the mean of a group of values, here > "wage_accepted". The group is determined by the stage and period, so in > the end there should be a column with the values of the wages in period 1 > stage1, period 1 stage 2, period 2 stage1... Unfortunately, I haven't much > of a clue on how to program this. Could you tell me how the function > should roughly look like ? if-else, loops included or not ? in the end ? >A couple of possible approaches .... # read in your data lines <-"treatment session period stage wage_accepted type 1 1 1 1 25 low 1 1 1 1 19 low 1 1 1 1 15 low 1 1 1 2 32 high 1 1 1 2 13 low 1 1 1 2 14 low 1 1 2 1 17 low 1 1 2 1 12 low" d <- read.table(textConnection(lines), header = TRUE) closeAllConnections() # 1. using the aggregate function aggregate(d$wage_accepted, list(period=d$period, stage=d$stage), FUN=mean) # 2. using the by function by(d$wage_accepted,list(d$period,d$stage),FUN=mean) As for tutorial resources, I would recommend visiting CRAN at http://cran.r-project.org/ ... follow the "Manuals" link on the left hand side of the page. HTH Pete -- View this message in context: http://r.789695.n4.nabble.com/how-to-calculate-the-mean-of-a-group-in-a-table-tp3505986p3506024.html Sent from the R help mailing list archive at Nabble.com.
On May 7, 2011, at 2:24 PM, tornanddesperate wrote:> Hi its me again > > I don't mean to get on your nerves, but the use of R proofs to be a > bit more > complicated than envisaged. > > I would like to calculate the mean of a group of values, here > "wage_accepted". The group is determined by the stage and period, so > in the > end there should be a column with the values of the wages in period 1 > stage1, period 1 stage 2, period 2 stage1... Unfortunately, I > haven't much > of a clue on how to program this. Could you tell me how the function > should > roughly look like ? if-else, loops included or not ? in the end ? > > treatment session period stage wage_accepted type > 1 1 1 1 1 25 low > 2 1 1 1 1 19 low > 3 1 1 1 1 15 low > 4 1 1 1 2 32 high > 5 1 1 1 2 13 low > 6 1 1 1 2 14 low > 7 1 1 2 1 17 low > 8 1 1 2 1 12 low > > I am also very grateful if someone could point out a good internet > tutorial > resource for R functions such as if-else and loops and how to use > them in > conjunction with tables. It should contain simple examples if > possible.I see Pete Brecknock has given you two workable answers with `by` and `aggregate`. According to the tapply help page, those functions are both based on tapply, and so it should be no surprise that tapply is also workable: tapply(d$wage_accepted, list(period=d$period, stage=d$stage), mean) stage period 1 2 1 19.66667 19.66667 2 14.50000 NA But results are returned as a table, which I find more usable since I can then use the inheritance of tables from matrix properties and divide tapply-summary calculations by the results of table() done with the same marginal factors. On the other hand aggregate can deliver multiple column results where tapply can only deliver the results from one column. Their argument requirements are somewhat different, eg. tapply and by do not need the name of the FUN argument but aggregate does, and I keep needing to refresh my memory on those. -- David Winsemius, MD West Hartford, CT
I see you already have three solutions but, just for the heck of it, here's another. I am trying to get familiar with the reshape2 package and your question was a good exercise for me. With your data set named xx: library(reshape2) yy <- melt(xx, id=c("period", "treatment", "session", "type","stage"), measured=c("wage_accepted")) dcast(yy, period + stage ~ variable, mean) As for your request for tutorials for loops and if-else materials, I cannot make any better suggestions than you already have. However, in R, if you are thinking of using these it usually means you are not thinking properly in R. There are "usually" faster and better was of achieving the desired results in R. Sometimes a loop or if-else is appropriate but usually in many fewer instances than in most other languages. By the way type is a reserved(? name in R. Not a problem here but it's safer not to use reserved words in R. --- On Sat, 5/7/11, tornanddesperate <MatthiasNeff at gmx.ch> wrote:> From: tornanddesperate <MatthiasNeff at gmx.ch> > Subject: [R] how to calculate the mean of a group in a table > To: r-help at r-project.org > Received: Saturday, May 7, 2011, 2:24 PM > Hi its me again > > I don't mean to get on your nerves, but the use of R proofs > to be a bit more > complicated than envisaged. > > I would like to calculate the mean of a group of values, > here > "wage_accepted". The group is determined by the stage and > period, so in the > end there should be a column with the values of the wages > in period 1 > stage1, period 1 stage 2, period 2 stage1... Unfortunately, > I haven't much > of a clue on how to program this. Could you tell me how the > function should > roughly look like ? if-else, loops included or not ? in > the end ? > > ???treatment session period stage > wage_accepted type > 1? ? ? ? ? 1? ? > ???1? ? ? 1? > ???1? ? ? ? ? ? > 25? low > 2? ? ? ? ? 1? ? > ???1? ? ? 1? > ???1? ? ? ? ? ? > 19? low > 3? ? ? ? ? 1? ? > ???1? ? ? 1? > ???1? ? ? ? ? ? > 15? low > 4? ? ? ? ? 1? ? > ???1? ? ? 1? > ???2? ? ? ? ? ? > 32 high > 5? ? ? ? ? 1? ? > ???1? ? ? 1? > ???2? ? ? ? ? ? > 13? low > 6? ? ? ? ? 1? ? > ???1? ? ? 1? > ???2? ? ? ? ? ? > 14? low > 7? ? ? ? ? 1? ? > ???1? ? ? 2? > ???1? ? ? ? ? ? > 17? low > 8? ? ? ? ? 1? ? > ???1? ? ? 2? > ???1? ? ? ? ? ? > 12? low > > I am also very grateful if someone could point out a good > internet tutorial > resource for R functions such as if-else and loops and how > to use them in > conjunction with tables. It should contain simple examples > if possible. > > As ever indepted to you, > > Matthias > > > > -- > View this message in context: http://r.789695.n4.nabble.com/how-to-calculate-the-mean-of-a-group-in-a-table-tp3505986p3505986.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. >
tornanddesperate
2011-May-10 14:59 UTC
[R] how to calculate the mean of a group in a table
Just wanted to thank everybody here for their great help. I tried the "tapply" and it worked brilliantly. By changing its parameters I can easily compute the values for the other treatments as well. Without help, I probably would just have despaired. Have a great time mat -- View this message in context: http://r.789695.n4.nabble.com/how-to-calculate-the-mean-of-a-group-in-a-table-tp3505986p3512145.html Sent from the R help mailing list archive at Nabble.com.