Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
147 views
in Technique[技术] by (71.8m points)

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 e.g. 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

question from:https://stackoverflow.com/questions/65905522/summing-up-different-elements-in-a-matrix-in-r

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Given your matrix, e.g.

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, ...


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...