How to find the mean of three-dimensional array in R?

R ProgrammingServer Side ProgrammingProgramming

A three-dimensional array can have matrices of different size and they are not necessarily to be square or rectangular. Also, all the elements in an array are of same data type. If we want to find the mean of a three-dimensional array then apply function can be used where we need to refer the columns and rows of the array elements using combination function.

Example

 Live Demo

A1<-array(c(1:4,5:8,9:12),c(2,2,3))
A1

Output

, , 1
[,1] [,2]
[1,] 1 3
[2,] 2 4
, , 2
[,1] [,2]
[1,] 5 7
[2,] 6 8
, , 3
[,1] [,2]
[1,] 9 11
[2,] 10 12
> apply(A1,c(1,2),mean)
[,1] [,2]
[1,] 5 7
[2,] 6 8

Example

A2<-array(c(rpois(4,1),rpois(4,5),rpois(4,8)),c(2,2,3))
A2

Output

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

Example

apply(A2,c(1,2),mean)

Output

[,1] [,2]
[1,] 3.666667 2.666667
[2,] 3.333333 5.000000

Example

 Live Demo

A3<-array(c(rpois(10,1),rpois(10,5),rpois(10,8)),c(2,5,3))
A3

Output

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

Example

<apply(A3,c(1,2),mean)

Output

[,1] [,2] [,3] [,4] [,5]
[1,] 4.333333 2.666667 6.666667 5.000000 3.666667
[2,] 3.000000 4.333333 5.666667 5.333333 3.666667

Example

 Live Demo

A4<-array(c(rnorm(25,1,0.25),rnorm(25,5,3.2),rnorm(25,125,8)),c(5,5,3))
A4

Output

, , 1
        [,1]      [,2]      [,3]      [,4]    [,5]
[1,] 0.9965280 1.2496879 1.0314247 0.708599 0.8077254
[2,] 0.7922626 1.0025704 0.7009782 1.142506 0.7445184
[3,] 1.0053649 0.5531278 1.4328092 1.385334 1.0892912
[4,] 0.9793857 1.3234864 0.5553419 1.215801 1.1023305
[5,] 1.3995579 1.0262346 1.2721514 1.394952 0.6184446
, , 2
        [,1]     [,2]     [,3]     [,4]     [,5]
[1,] 6.174299 6.038592 12.470821 1.722607 1.069005
[2,] 3.789770 8.430932 8.747260 7.552801 3.886604
[3,] 1.730295 2.493142 3.465323 3.507519 3.834119
[4,] 6.075908 7.348407 4.118381 6.081857 4.167369
[5,] 4.130724 6.829041 7.469443 3.409544 5.808045
, , 3
       [,1]      [,2]      [,3]    [,4]    [,5]
[1,] 110.6113 119.8429 121.6303 122.9954 140.5715
[2,] 125.5317 122.1797 135.2451 119.5165 122.9406
[3,] 124.0559 119.0249 135.1922 114.0559 112.1363
[4,] 118.1446 119.0530 134.6688 118.8334 124.4825
[5,] 120.2941 109.3362 119.8095 129.1414 127.6529

Example

apply(A4,c(1,2),mean)

Output

       [,1]       [,2]    [,3]    [,4]    [,5]
[1,] 39.26071 42.37706 45.04419 41.80885 47.48274
[2,] 43.37124 43.87108 48.23111 42.73726 42.52389
[3,] 42.26385 40.69038 46.69677 39.64958 39.01991
[4,] 41.73330 42.57496 46.44750 42.04369 43.25072
[5,] 41.94148 39.06383 42.85037 44.64863 44.69313

Example

 Live Demo

A5<-array(c(rexp(25,1.25),rexp(25,3.2),rexp(25,1.37)),c(5,5,3))
A5

Output

, , 1
      [,1]       [,2]       [,3]       [,4]    [,5]
[1,] 1.3964752 0.14365375 0.1244120 0.006844883 0.1649243
[2,] 0.4146687 0.74798876 1.3172895 0.273072680 1.9675031
[3,] 0.5640029 0.05317204 0.1365023 1.139974748 0.4691554
[4,] 3.4377135 0.40737835 0.1296337 3.827344957 0.3062992
[5,] 0.5489296 0.68912311 0.9960337 1.891826684 0.6505368
, , 2
         [,1]       [,2]       [,3]       [,4]    [,5]
[1,] 0.461391444 0.14704248 1.7219179 0.26808968 0.01277777
[2,] 0.002633461 0.02843441 0.5924807 0.59324483 1.34762650
[3,] 0.296192070 0.38350989 0.3453407 0.14514950 0.21145723
[4,] 0.137173681 0.34683067 0.9046680 0.05949873 0.42690397
[5,] 0.171683422 0.12761626 0.3183557 0.01596812 0.04222600
, , 3
      [,1]       [,2]       [,3]       [,4]       [,5]
[1,] 0.7641526 0.78719016 0.96282362 0.02212902 0.3261842
[2,] 1.1584192 1.20147453 0.03766346 0.30549609 1.1452613
[3,] 0.5849248 1.97291039 0.52678525 0.78435948 0.0107882
[4,] 2.1916248 0.09176414 1.40056593 1.11841176 0.7624021
[5,] 0.1565796 1.35400081 0.26728881 0.36045571 0.3076494

Example

apply(A5,c(1,2),mean)

Output

      [,1]       [,2]       [,3]       [,4]       [,5]
[1,] 0.8740064 0.3592955 0.9363845 0.09902119 0.1679621
[2,] 0.5252405 0.6592992 0.6491446 0.39060453 1.4867970
[3,] 0.4817066 0.8031974 0.3362094 0.68982791 0.2304670
[4,] 1.9221707 0.2819911 0.8116226 1.66841848 0.4985351
[5,] 0.2923976 0.7235801 0.5272261 0.75608351 0.3334708
raja
Published on 05-Dec-2020 17:55:26
Advertisements