I want to create a matrix (A) where its elements are the average of every four rows of another matrix (B). For example, the elements of row 1 in matrix A should be the averages of row 1 to 4 in matrix B. Currently I have used a loop function to get that but the size of the matrices are so large and that makes the loop very time consuming. I wonder if there is a better way to do that. Here is an example
B = matrix(runif(10000, 0, 10), 100, 100)
A = matrix(0, floor(dim(B)[1]/4), dim(B)[2])
for (im in 1: floor(dim(B)[1]/4)){
A[im, ] = colMeans(as.matrix(B[c((((im - 1)*4) + 1):(im*4)), ]))
}
You could vectorize this pretty easily using the rowsum
function which has a matrix
method (its' default) and can calculate sums by group. Then, just divide by 4 in order to get the means
grps <- floor(dim(B)[1]/4)
rowsum.default(B[1:(grps*4),], rep(1:grps, each = 4), reorder = FALSE)/4
Benchmarks
Since this is an optimization question here are some benchmarks with all the proposed methods on not such a big data set
library(zoo)
library(microbenchmark)
set.seed(123)
B <- matrix(runif(100, 0, 10), 10000, 100)
OP <- function(B) {
grps <- floor(dim(B)[1]/4)
A = matrix(0, grps, dim(B)[2])
for (im in 1: grps){
A[im, ] = colMeans(as.matrix(B[c((((im - 1)*4) + 1):(im*4)), ]))
}
A
}
DA <- function(B){
grps <- floor(dim(B)[1]/4)
rowsum.default(B[1:(grps*4),], rep(1:grps, each = 4), reorder = FALSE)/4
}
JB <- function(B) as.matrix(aggregate(B, list(gl(ceiling(nrow(B)/4), 4, nrow(B))), mean)[, -1])
Thela <- function(B) tapply(B, list((row(B)-1) %/% 4,col(B)), FUN=mean)
RollApply <- function(B) rollapply(B, width = 4, by = 4, FUN = mean, by.column = TRUE)
microbenchmark(OP(B), DA(B), JB(B), RollApply(B), Thela(B), times = 10L)
# Unit: milliseconds
# expr min lq mean median uq max neval cld
# OP(B) 45.57121 48.93491 70.17095 55.77107 65.43564 168.7760 10 a
# DA(B) 10.60941 10.87035 11.65232 11.36478 12.07908 14.1551 10 a
# JB(B) 1753.39114 1773.83230 1868.60788 1837.47161 1900.38141 2076.5835 10 b
# RollApply(B) 8946.90359 9009.45160 9380.62408 9294.98441 9450.16426 10922.2595 10 d
# Thela(B) 4820.36079 4925.70055 5117.22822 5048.89781 5257.58619 5650.2391 10 c
Turns out OPs solution isn't so bad after all.
You can achieve this with the following package (zoo) and function (rollapply).
install.packages("zoo")
require(zoo)
B <- matrix(runif(100, 0, 10),10, 10)
# with for loop
A = matrix(0,floor(dim(B)[1]/4),dim(B)[2])
for (im in 1 : floor(dim(B)[1]/4)){
+ A[im,] = colMeans(as.matrix(B[c((((im-1)*4)+1):(im*4)),]))}
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
[1,] 5.633970 4.092848 3.793473 5.437288 6.316069 4.714015 5.837214 7.150007 4.638332
[2,] 5.445271 2.024052 6.096939 6.165723 3.049140 4.928087 5.433291 5.674594 4.607373
[,10]
[1,] 5.260153
[2,] 6.589873
# with rowsum @ David
C = grps <- floor(dim(B)[1]/4)
rowsum(B[1:(grps*4),], rep(1:grps, each = 4))/4
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
1 5.633970 4.092848 3.793473 5.437288 6.316069 4.714015 5.837214 7.150007 4.638332
2 5.445271 2.024052 6.096939 6.165723 3.049140 4.928087 5.433291 5.674594 4.607373
[,10]
1 5.260153
2 6.589873
# With rollapply
D = rollapply(B, width = 4, by = 4, FUN = mean, by.column = T)
D
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
[1,] 5.633970 4.092848 3.793473 5.437288 6.316069 4.714015 5.837214 7.150007 4.638332
[2,] 5.445271 2.024052 6.096939 6.165723 3.049140 4.928087 5.433291 5.674594 4.607373
[,10]
[1,] 5.260153
[2,] 6.589873
aggregate
can do this too, but requires subsequent coercion to a matrix
:
as.matrix(aggregate(B, list(gl(ceiling(nrow(B)/4), 4, nrow(B))), mean)[, -1])
Note that if nrow(B)
isn't a multiple of 4, the result will include a final row that contains the column averages of the last nrow(B) %% 4
rows.
As indicated by @thelatemail, tapply
can do a neater job of this:
tapply(B, list((row(B)-1) %/% 4,col(B)), FUN=mean)