# Calculate Sum of All Matrix Objects in a List in R (Example Code)

In this post, I’ll illustrate how to calculate the sum of all matrix objects in a list in the R programming language.

## Example Data

x <- list(matrix(1:9, nrow = 3), # Construct list of matrix objects matrix(2:10, nrow = 3), matrix(3:11, nrow = 3), matrix(4:12, nrow = 3)) x # [[1]] # [,1] [,2] [,3] # [1,] 1 4 7 # [2,] 2 5 8 # [3,] 3 6 9 # # [[2]] # [,1] [,2] [,3] # [1,] 2 5 8 # [2,] 3 6 9 # [3,] 4 7 10 # # [[3]] # [,1] [,2] [,3] # [1,] 3 6 9 # [2,] 4 7 10 # [3,] 5 8 11 # # [[4]] # [,1] [,2] [,3] # [1,] 4 7 10 # [2,] 5 8 11 # [3,] 6 9 12 |

x <- list(matrix(1:9, nrow = 3), # Construct list of matrix objects matrix(2:10, nrow = 3), matrix(3:11, nrow = 3), matrix(4:12, nrow = 3)) x # [[1]] # [,1] [,2] [,3] # [1,] 1 4 7 # [2,] 2 5 8 # [3,] 3 6 9 # # [[2]] # [,1] [,2] [,3] # [1,] 2 5 8 # [2,] 3 6 9 # [3,] 4 7 10 # # [[3]] # [,1] [,2] [,3] # [1,] 3 6 9 # [2,] 4 7 10 # [3,] 5 8 11 # # [[4]] # [,1] [,2] [,3] # [1,] 4 7 10 # [2,] 5 8 11 # [3,] 6 9 12

## Example: Calculating the Sum of Matrices in a List Using the Reduce() Function

Reduce("+", x) # Get sum of all matrices # [,1] [,2] [,3] # [1,] 10 22 34 # [2,] 14 26 38 # [3,] 18 30 42 |

Reduce("+", x) # Get sum of all matrices # [,1] [,2] [,3] # [1,] 10 22 34 # [2,] 14 26 38 # [3,] 18 30 42

## Related Articles

Below, you can find some further resources on topics such as descriptive statistics and lists.