Spencer Jones
2006-Aug-16 17:36 UTC
[R] separate row averages for different parts of an array
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]]
Marc Schwartz (via MN)
2006-Aug-16 18:35 UTC
[R] separate row averages for different parts of an array
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, > > SpencerSomething 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
vincent at 7d4.com
2006-Aug-17 05:23 UTC
[R] separate row averages for different parts of an array
Spencer Jones a ?crit :> 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?rowSums, ?rowMeans something like : rowMeans(my_array[,1:100]) (perhaps you'll have to use t() also) hih
Gabor Grothendieck
2006-Aug-17 07:53 UTC
[R] separate row averages for different parts of an array
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. >