[R] Best way to calculate averages of Blocks in an matrix?

Keith S Weintraub kw1958 at gmail.com
Wed Apr 17 18:54:12 CEST 2013

```Folks,
I recently was given a simulated data set like the following subset:

sim_sub<-structure(list(V11 = c(0.01, 0, 0, 0.01, 0, 0.01, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0), V12 = c(0, 0, 0, 0.01, 0.03, 0,
0, 0, 0, 0, 0, 0.01, 0, 0.01, 0, 0, 0, 0, 0, 0.04), V13 = c(0,
0, 0, 0.01, 0, 0, 0, 0, 0, 0.01, 0, 0, 0, 0, 0.01, 0, 0, 0, 0,
0.01), V14 = c(0, 0.01, 0.01, 0.01, 0.01, 0, 0, 0, 0, 0.03, 0,
0, 0.01, 0.01, 0.04, 0.01, 0.02, 0, 0.01, 0.03), V15 = c(0, 0.01,
0, 0, 0.01, 0, 0, 0, 0.01, 0.02, 0.01, 0, 0, 0.01, 0, 0, 0, 0.01,
0.01, 0.04), V16 = c(0, 0, 0, 0.03, 0.02, 0.01, 0, 0, 0.02, 0.02,
0, 0.02, 0.02, 0, 0.01, 0.01, 0, 0, 0.03, 0.01), V17 = c(0, 0.01,
0, 0.01, 0, 0, 0, 0.01, 0.05, 0.03, 0, 0.01, 0, 0.02, 0.02, 0,
0, 0.01, 0.02, 0.04), V18 = c(0, 0.01, 0, 0.03, 0.03, 0, 0, 0,
0.02, 0.01, 0, 0.02, 0.01, 0.02, 0.03, 0.02, 0, 0, 0.04, 0.04
), V19 = c(0, 0.01, 0.01, 0.02, 0.07, 0, 0, 0, 0.04, 0.01, 0.02,
0, 0, 0, 0.04, 0, 0, 0, 0, 0.05), V20 = c(0, 0, 0, 0.01, 0.04,
0.01, 0, 0, 0.02, 0.04, 0.01, 0, 0.02, 0, 0.03, 0, 0.02, 0.01,
0.03, 0.03)), .Names = c("V11", "V12", "V13", "V14", "V15", "V16",
"V17", "V18", "V19", "V20"), row.names = c(NA, 20L), class = "data.frame")

> sim_sub
V11  V12  V13  V14  V15  V16  V17  V18  V19  V20
1  0.01 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
2  0.00 0.00 0.00 0.01 0.01 0.00 0.01 0.01 0.01 0.00
3  0.00 0.00 0.00 0.01 0.00 0.00 0.00 0.00 0.01 0.00
4  0.01 0.01 0.01 0.01 0.00 0.03 0.01 0.03 0.02 0.01
5  0.00 0.03 0.00 0.01 0.01 0.02 0.00 0.03 0.07 0.04
6  0.01 0.00 0.00 0.00 0.00 0.01 0.00 0.00 0.00 0.01
7  0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
8  0.00 0.00 0.00 0.00 0.00 0.00 0.01 0.00 0.00 0.00
9  0.00 0.00 0.00 0.00 0.01 0.02 0.05 0.02 0.04 0.02
10 0.00 0.00 0.01 0.03 0.02 0.02 0.03 0.01 0.01 0.04
11 0.00 0.00 0.00 0.00 0.01 0.00 0.00 0.00 0.02 0.01
12 0.00 0.01 0.00 0.00 0.00 0.02 0.01 0.02 0.00 0.00
13 0.00 0.00 0.00 0.01 0.00 0.02 0.00 0.01 0.00 0.02
14 0.00 0.01 0.00 0.01 0.01 0.00 0.02 0.02 0.00 0.00
15 0.00 0.00 0.01 0.04 0.00 0.01 0.02 0.03 0.04 0.03
16 0.00 0.00 0.00 0.01 0.00 0.01 0.00 0.02 0.00 0.00
17 0.00 0.00 0.00 0.02 0.00 0.00 0.00 0.00 0.00 0.02
18 0.00 0.00 0.00 0.00 0.01 0.00 0.01 0.00 0.00 0.01
19 0.00 0.00 0.00 0.01 0.01 0.03 0.02 0.04 0.00 0.03
20 0.00 0.04 0.01 0.03 0.04 0.01 0.04 0.04 0.05 0.03

Every 5 rows represents one block of simulated data.

What would be the best way to average the blocks?

My way was to reshape sim_sub, average over the columns and then reshape back like so:

> matrix(colSums(matrix(t(sim_sub), byrow = TRUE, ncol = 50)), byrow = TRUE, ncol = 10)/4
[,1]   [,2]   [,3]   [,4]   [,5]  [,6]   [,7]   [,8]   [,9]  [,10]
[1,] 0.0050 0.0000 0.0000 0.0025 0.0025 0.005 0.0000 0.0050 0.0050 0.0050
[2,] 0.0000 0.0025 0.0000 0.0075 0.0025 0.005 0.0050 0.0075 0.0025 0.0050
[3,] 0.0000 0.0000 0.0000 0.0050 0.0025 0.005 0.0050 0.0025 0.0025 0.0075
[4,] 0.0025 0.0050 0.0025 0.0075 0.0075 0.020 0.0250 0.0275 0.0150 0.0150
[5,] 0.0000 0.0175 0.0075 0.0275 0.0175 0.015 0.0225 0.0275 0.0425 0.0350

How bad is "t(sim_sub)" in the above?