1

I am trying to create a matrix where each row consists of the sum of every three rows in another matrix. There are actually a bunch of these matrices in a list and I am performing the same operation on each of the elements in that list. Based on this post I was able to generate the code below. It works but it takes forever for my more complicated data set.

test<-lapply(1:1000, function(x) matrix(1:300, nrow=60))
testCons<-lapply(test, function(x) apply(x, 2, function(y) tapply(y, ceiling(seq_along(y)/3), sum)))

Does anybody have an idea of how to speed that up or simplify it?

Sotos
  • 51,121
  • 6
  • 32
  • 66
user3390169
  • 1,015
  • 1
  • 11
  • 34

1 Answers1

4

rowsum gives an easy speed up - it calculates the sum of rows according to a grouping variable, which is an index for every three rows.

 test <- lapply(1:1000, function(x) matrix(1:300, nrow=60))

 system.time(
   testCons <- lapply(test, function(x) apply(x, 2, function(y) tapply(y, ceiling(seq_along(y)/3), sum)))
 )
#   user  system elapsed 
#  1.672   0.004   1.678 

 system.time(
   testCons2 <- lapply(test, function(x) rowsum(x, rep(seq_len(nrow(x) / 3), each=3)))
 )
#   user  system elapsed 
#   0.08    0.00    0.08 

 all.equal(testCons, testCons2)
#[1] TRUE
user20650
  • 24,654
  • 5
  • 56
  • 91