Re: [R] separate row averages for different parts of an array

From: Marc Schwartz (via MN) <mschwartz_at_mn.rr.com>
Date: Thu 17 Aug 2006 - 04:35:18 EST

On Wed, 2006-08-16 at 11:36 -0600, Spencer Jones wrote:
> I have an array with 44800 columns and 24 rows I would like to compute the
> row average for the array 100 columns at a time, so I would like to end up
> with an array of 24 rows x 448 columns. I have tried using apply(dataset, 1,
> function(x) mean(x[])), but I am not sure how to get it to take the average
> 100 columns at a time. Any ideas would be welcomed.
>
> thanks,
>
> Spencer

Something along the lines of the following, presuming that 'mat' is your 24 * 44800 matrix:

  sapply(seq(1, 44800, 100), function(x) rowMeans(mat[, x:(x + 99)]))

The first argument in sapply() creates a sequence from 1:44800 by increments of 100.

sapply() then passes each value in the sequence as the starting index value 'x' to use to subset 'mat' in 100 column sets and gets the rowMeans() for each sub-matrix.

The returned object will be a 24 * 448 matrix.

HTH, Marc Schwartz



R-help@stat.math.ethz.ch 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. Received on Thu Aug 17 04:43:49 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Thu 17 Aug 2006 - 06:24:46 EST.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.