[R] get mean of several rows

Thomas Lumley tlumley at u.washington.edu
Thu Dec 4 17:31:26 CET 2003


On Thu, 4 Dec 2003, Jan Wantia wrote:

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

There is also a function rowsum() for doing precisely this (it's quite a
bit faster than the tapply solution).

	-thomas




More information about the R-help mailing list