Re: [R] how to calculate the mean of a group in a table

From: Pete Brecknock <Peter.Brecknock_at_bp.com>
Date: Sat, 07 May 2011 11:56:02 -0700 (PDT)

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.

______________________________________________
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.
Received on Sat 07 May 2011 - 19:04:18 GMT

This quarter's messages: by month, or sorted: [ by date ] [ by thread ] [ by subject ] [ by author ]

All messages

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Sat 07 May 2011 - 19:20:06 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive