Store your 'matrix' as a data frame.  Call it 'SchoolVals' say.  Then

SchoolMeans <- with(SchoolVals, tapply(value, school, mean))

should do it.  If you have missing values you want to ignore:

SchoolMeans <- with(SchoolVals, tapply(value, school, mean, na.rm = TRUE))




Bill Venables
http://www.cmis.csiro.au/bill.venables/ 


-----Original Message-----
From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On 
Behalf Of cindy Guo
Sent: Monday, 9 February 2009 1:32 PM
To: r-help@r-project.org
Subject: [R] question about mean

Hello, everyone,

I have a matrix like following:

school  value
A          .1
A          .2
A          .15
A          .2
B          .3
B          .5
C          .3
C          .3
C          .4
C          .5
C          .6
C          .9
C          1

I want to get the mean 'value' for each 'school', but each school has
different length. Is there any way to do this fast? Because my data has
hundreds of thousands of rows.

Thank you,

Cindy

        [[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.

______________________________________________
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.

Reply via email to