69

I have a list where each element is a 5*5 matrix. Eg

[[1]]   
           V1          V2          V3          V4          V5      
      [1,]   0.000000   46.973700   21.453500  338.547000   10.401600 
      [2,]  43.020500    0.000000  130.652000  840.526000   56.363700 
      [3,]  12.605600  173.238000    0.000000  642.075000   19.628100 
      [4,] 217.946000  626.368000  481.329000    0.000000  642.341000 
      [5,] 217.946000  626.368000  481.329000    0.000000  642.341000 
[[2]]   
           V1          V2          V3          V4          V5      
      [1,]   0.000000   47.973700   21.453500  338.547000   10.401600 
      [2,]  143.020500    0.000000  130.652000  840.526000   56.363700 
      [3,]  312.605600  17.238000    0.000000  642.075000   19.628100 
      [4,]  17.946000  126.368000  481.329000    0.000000  642.341000
      [5,] 217.946000  626.368000  481.329000    0.000000  642.341000  
...

How can I use an apply-like function to sum matrix [1] to [n], and return a 5*5 matrix as a result (each element is a sum of the corresponding elements in each of the matrix in the list) ?

3
  • I suggest you edit your post to give a reproducible example and make clearer what you're after. An example of what your final result would like like would also be helpful. In it's current state it will likely be closed. Jul 25, 2012 at 2:08
  • Thanks for your suggestion. My post has been edited.
    – Seen
    Jul 25, 2012 at 2:14
  • Are you summing a list of 5 * 5 matrices? to give a 5 *5 matrix?
    – mnel
    Jul 25, 2012 at 2:25

3 Answers 3

120

Use Reduce.

## dummy data

.list <- list(matrix(1:25, ncol = 5), matrix(1:25, ncol = 5))

Reduce('+', .list)
##       [,1] [,2] [,3] [,4] [,5]
## [1,]    2   12   22   32   42
## [2,]    4   14   24   34   44
## [3,]    6   16   26   36   46
## [4,]    8   18   28   38   48
## [5,]   10   20   30   40   50
6
  • That is nice! Thank you very much!
    – Seen
    Jul 25, 2012 at 2:25
  • @GSee I tried to used the do.call, but it returns an error as "operator needs one or two arguments"
    – Seen
    Jul 25, 2012 at 2:32
  • 1
    do.call will only work with a list of length 2. I've removed this from the answer.
    – mnel
    Jul 25, 2012 at 2:36
  • Another function I've never seen! Thanks! Jun 15, 2013 at 15:18
  • What would be the equivalent of '+' to get the max? Feb 23, 2018 at 10:57
15

I think @mnel's answer is the more efficient but this is another approach:

apply(simplify2array(.list), c(1,2), sum)

    [,1] [,2] [,3] [,4] [,5]
[1,]    2   12   22   32   42
[2,]    4   14   24   34   44
[3,]    6   16   26   36   46
[4,]    8   18   28   38   48
[5,]   10   20   30   40   50
3
  • 1
    Nice trick though in this case Reduce is faster (Reduce is the fastest answer here that extends to multiple correlation matrices in a list). +1 Jul 25, 2012 at 21:36
  • 1
    And to the contrary of Reduce, it also works with mean instead of +.
    – nassimhddd
    Mar 4, 2013 at 16:30
  • 2
    this also gives options like na.rm. +1
    – mts
    Jul 22, 2015 at 13:39
5

You could you do.call with some monkeying around but it loses its eloquence:

.list <- list(matrix(1:25, ncol=5), matrix(1:25,ncol=5), matrix(1:25,ncol=5))

x <- .list[[1]]
lapply(seq_along(.list)[-1], function(i){
    x <<- do.call("+", list(x, .list[[i]]))
})
x
2
  • 1
    But, then you don't need do.call; you could have x <<- x + .list[[i]] inside your lapply.
    – GSee
    Jul 25, 2012 at 3:06
  • 1
    very true. Reduce is much more eloquent though on a bunch of matrices the lapply approach may be faster (sometimes the Higher Order Functions suffer speed issues). Jul 25, 2012 at 3:13

Not the answer you're looking for? Browse other questions tagged or ask your own question.