简体   繁体   中英

Summing up different elements in a matrix in R

I'm trying to perform calculations on different elements in a matrix in R. My Matrix is 18x18 and I would like to get eg the mean of each 6x6 array (which makes 9 arrays in total). My desired arrays would be:

A1 <- df[1:6,1:6]
A2 <- df[1:6,7:12]
A3 <- df[1:6,13:18]
B1 <- df[7:12,1:6]
B2 <- df[7:12,7:12]
B3 <- df[7:12,13:18]
C1 <- df[13:18,1:6]
C2 <- df[13:18,7:12]
C3 <- df[13:18,13:18]

The matrix looks like this:

    5   10  15  20  25  30  35  40  45  50  55  60  65  70  75  80  85  90
5   14  17  9   10  8   4   10  12  18  9   13  14  NA  NA  19  15  10  10
10  30  32  23  27  17  28  25  12  28  29  28  26  19  25  34  24  11  17
15  16  16  16  9   17  27  17  16  30  13  18  13  15  13  19  8   7   9
20  15  12  18  18  18  6   4   6   9   11  10  10  13  11  8   10  15  15
25  7   13  21  7   3   5   2   5   5   4   3   2   3   5   2   1   5   6
30  5   9   1   7   7   4   4   12  8   9   2   0   5   2   1   0   2   6
35  3   0   2   0   0   4   4   7   4   4   5   2   0   0   1   0   0   0
40  0   4   0   0   0   1   3   9   10  10  1   0   0   0   1   0   1   0
45  0   0   0   0   0   3   10  9   17  9   1   0   0   0   0   0   0   0
50  0   0   2   0   0   0   2   8   20  0   0   0   0   0   1   0   0   0
55  0   0   0   0   0   0   7   3   21  0   0   0   0   0   0   0   0   0
60  0   0   0   0   3   4   10  2   2   0   0   1   0   0   0   0   0   0
65  0   0   0   0   0   4   8   4   8   11  0   0   0   0   0   0   0   0
70  0   0   0   0   0   6   2   5   14  0   0   0   0   0   0   0   0   0
75  0   0   0   0   0   4   0   5   9   0   0   0   0   0   0   0   0   0
80  0   0   0   0   0   4   4   0   4   2   0   0   0   0   0   0   0   0
85  0   0   0   0   0   0   0   4   1   1   0   0   0   0   0   0   0   0
90  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0

Is there a clean way to solve this issue with a loop?

Thanks a lot in advance,

Paul

Here's one way:

# generate fake data
set.seed(47)
n = 18
m = matrix(rpois(n * n, lambda = 5), nrow = n)

# generate starting indices
n_array = 6
start_i = seq(1, n, by = n_array)
arr_starts = expand.grid(row = start_i, col = start_i)

# calculate sums
with(arr_starts, mapply(function(x, y) sum(m[(x + 1:n_array) - 1, (y + 1:n_array) - 1]), row, col))
# [1] 158 188 176 201 188 201 197 206 204

Given your matrix, eg

x <- matrix(1:(18*18), ncol=18)

Try, for example for sub matrices of 6

step <- 6

nx <- nrow(x)
if((nx %% step) != 0) stop("nx %% step should be 0") 

indI <- seq(1, nx, by=step)
nbStep <- length(indI)

for(Col in 1:nbStep){
  for(Row in 1:nbStep){
    name <- paste0(LETTERS[Col],Row)
    theCol <- indI[Col]:(indI[Col]+step-1)
    theRow <- indI[Row]:(indI[Row]+step-1)
    assign(name, sum(x[theCol, theRow]))
  }
}

You'll get your results in A1, A2, A3... This is the idea. Twist the code for non square matrices, different size of sub matrices, ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM