[R] question about mean

Bill.Venables at csiro.au Bill.Venables at csiro.au
Mon Feb 9 04:40:27 CET 2009

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

-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of cindy Guo
Sent: Monday, 9 February 2009 1:32 PM
To: r-help at 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,


	[[alternative HTML version deleted]]

R-help at r-project.org mailing list
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

More information about the R-help mailing list