index array, summation

Which code would add the first raw , first column from each of the arrays? I need to get their sum:
, , 1

      [,1]      [,2]      [,3]      [,4]      [,5]

[1,] 8.747092 8.359063 13.023562 9.910133 11.837955
[2,] 10.367287 10.974858 10.779686 9.967619 11.564273
[3,] 8.328743 11.476649 8.757519 11.887672 10.149130
[4,] 13.190562 11.151563 5.570600 11.642442 6.021297
[5,] 10.659016 9.389223 12.249862 11.187803 11.239651

, , 2

      [,1]      [,2]      [,3]      [,4]      [,5]

[1,] 9.887743 12.717359 9.170011 9.670953 8.585010
[2,] 9.688409 9.794425 9.211420 9.493277 10.729164
[3,] 7.058495 10.775343 9.881373 11.393927 11.537066
[4,] 9.043700 9.892390 12.200051 11.113326 9.775308
[5,] 10.835883 7.245881 11.526351 8.622489 11.762215

, , 3

      [,1]      [,2]      [,3]      [,4]      [,5]

[1,] 10.796212 13.960800 14.803236 10.377585 10.951019
[2,] 8.775947 9.265557 9.921520 6.390083 8.580107
[3,] 10.682239 7.911731 11.379479 12.931110 11.221453
[4,] 7.741274 11.139439 10.056004 10.306507 8.131805
[5,] 12.866047 9.729891 8.513454 14.345223 7.492733

, , 4

      [,1]      [,2]      [,3]     [,4]      [,5]

[1,] 10.582892 8.862663 10.665901 8.91496 11.116973
[2,] 9.113416 9.729643 12.126200 12.41574 7.446816
[3,] 10.002211 12.356174 9.391632 12.32081 8.853469
[4,] 10.148683 6.952866 10.740038 11.40043 7.550775
[5,] 8.820958 11.187892 10.534198 13.17367 9.053199

, , 5

      [,1]     [,2]      [,3]      [,4]      [,5]

[1,] 8.759267 13.53457 8.728527 9.214384 8.988085
[2,] 10.084232 11.43341 9.076711 9.360014 12.686078
[3,] 8.178157 11.82035 12.864564 9.441773 9.570841
[4,] 10.316058 10.76837 8.698607 10.988377 9.640887
[5,] 8.690831 13.36435 9.585239 9.645339 9.799619

The following code returns separate sums for the first row and the first column. Is that what you want?

Arry <- array(1:27,dim = c(3,3,3))
Arry
#> , , 1
#> 
#>      [,1] [,2] [,3]
#> [1,]    1    4    7
#> [2,]    2    5    8
#> [3,]    3    6    9
#> 
#> , , 2
#> 
#>      [,1] [,2] [,3]
#> [1,]   10   13   16
#> [2,]   11   14   17
#> [3,]   12   15   18
#> 
#> , , 3
#> 
#>      [,1] [,2] [,3]
#> [1,]   19   22   25
#> [2,]   20   23   26
#> [3,]   21   24   27
Sumfunc <- function(Ar){
  c(FirstRow = sum(Ar[1,]), FirstCol = sum(Ar[,1]))
}
SUMS <- apply(Arry, MARGIN = 3, FUN = Sumfunc)
SUMS
#>          [,1] [,2] [,3]
#> FirstRow   12   39   66
#> FirstCol    6   33   60

Created on 2021-04-19 by the reprex package (v0.3.0)

screen.pdf (9.4 KB)

Hi, I need to sum up the green areas.

Use apply. Here's an example with FJCC's data:

> Arry <- array(data = 1:27, dim = c(3, 3, 3))
> Arry
, , 1

     [,1] [,2] [,3]
[1,]    1    4    7
[2,]    2    5    8
[3,]    3    6    9

, , 2

     [,1] [,2] [,3]
[1,]   10   13   16
[2,]   11   14   17
[3,]   12   15   18

, , 3

     [,1] [,2] [,3]
[1,]   19   22   25
[2,]   20   23   26
[3,]   21   24   27

> apply(X = Arry, MARGIN = c(1, 2), sum)
     [,1] [,2] [,3]
[1,]   30   39   48
[2,]   33   42   51
[3,]   36   45   54
> apply(X = Arry, MARGIN = c(1, 2), sum)[1, 1]
[1] 30

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.