Skip to content

question about mean

4 messages · cindy Guo, Bill Venables, Rolf Turner +1 more

#
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-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,

Cindy


______________________________________________
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.
#
On 9/02/2009, at 4:40 PM, Bill.Venables at csiro.au wrote:

            
Surely it's a data frame already, since ``school'' is character or  
factor,
	and ``value'' is (must be?) numeric.

	People have this unfortunate predilection to refer to data frames as
	matrices, and this messes them, and everybody else, up.

		cheers,

			Rolf


<snip>

######################################################################
Attention:\ This e-mail message is privileged and confid...{{dropped:9}}
#
Rolf Turner wrote:
still, a naive user's messing up data frames with matrices seems no
worse than an experrienced (?) r programmer's messing up lists with vectors.

vQ