[R] get mean of several rows

Gabor Grothendieck ggrothendieck at myway.com
Thu Dec 4 14:56:47 CET 2003



The following:

   c( tapply( x, (row(x)-1)%/%5, mean ) )

gives a vector whose first element is the mean of every
element in the rows 1 through 5 inclusive, whose second 
element is the mean of every element rows 6 through 10 
inclusive, etc.

--- 
Date: 4 Dec 2003 14:03:57 +0100 
From: Jan Wantia <wantia at ifi.unizh.ch>
To: <r-help at stat.math.ethz.ch> 
Subject: [R] get mean of several rows 

 
 
Dear all!

After hours of trying around, I gave up:

I have a 2-dimensional array, and I know how to split it into its rows 
and how to get the mean for every row using 'sapply'.
But what I want is to calculate the mean over the first n rows, and then 
the second n rows, etc., so that I get a vector like:

v == mean1(row 1:5), mean2(row6:10),...

(trivial, you might say. I find it rather mind-boggling, though: I tried 
to get the mean from the array before splitting it, after splitting it, 
looping through it with for-loops...I feel like an idiot by now; looks 
like I missed a crucial point of how 'R' works.)

Thanks a lot in advance!
-- 

______________________________________________________

Jan Wantia
Dept. of Information Technology, University of Zürich
Andreasstr. 15
CH 8050 Zürich
Switzerland




More information about the R-help mailing list