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

Gabor Grothendieck ggrothendieck at gmail.com
Thu Aug 17 09:53:51 CEST 2006


The following reshapes mat so we can take the means of the columns
of the resulting 3d array and then transposes it back to the original
orientation:

   t(colMeans(array(t(mat), c(100, 448, 24))))

You might want to try it on this test set first where anscombe
is an 11x8 data set built into R.  Here are 4 solutions using
anscombe

1.   This is just the above written for the anscombe data set:

t(colMeans(array(t(anscombe), c(4,2,11))))

2. Here is a solution using apply instead of colMeans and t.
In this case anscombe is a data.frame, not an array/matrix,
and we need to turn it into one first.  The prior solution
also required a matrix but tranpose will convert a dataframe
to a matrix so we did not have to explicitly do it there.  If
your array is indeed an array as stated in your post then
you can omit the as.matrix part.  In your case the
c(11,4,2) vector would be c(24, 100, 448) :

apply(array(as.matrix(anscombe), c(11,4,2)), c(1,3), mean)

3. Here is another solution.  This one uses the zoo package
and does have the advantage of not having to specify a
bunch of dimensions.  It uses rapply from zoo (which will
be renamed rollapply in the next version of zoo so as not
to conflict with the new rapply that is appearing in R 2.4.0).
In your case both occurrences of 4 would be 100:

library(zoo)
coredata(t(rapply(zoo(t(anscombe)), 4, by = 4, mean)))

4. This is Marc's solution except we use seq instead of : at
the end in order to make use of the length= argument.
In your case c(11, 8, 4) would be c(1, 44800, 100) and length = 4
would be length = 100:

sapply(seq(1, 8, 4), function(i) rowMeans(anscombe[, seq(i, length = 4)]))





On 8/16/06, Spencer Jones <ssj1364 at gmail.com> 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
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at 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.
>



More information about the R-help mailing list