How to find the variance of row elements of a matrix in R?


Finding the variance of columns is a common task in data analysis but often data is provided in wide format instead of long format, therefore, the cases are represented vertically and the variables are aligned horizontally and this data could be available in matrix or any other form. Therefore, the variance can be easily found by using apply function.

Example

 Live Demo

M1<-matrix(1:25,ncol=5)
M1

Output

   [,1] [,2] [,3] [,4] [,5]
[1,] 1   6    11   16    21
[2,] 2   7    12   17    22
[3,] 3   8    13   18    23
[4,] 4   9    14   19    24
[5,] 5   10   15   20    25

Example

apply(M1,1,var)

Output

[1] 62.5 62.5 62.5 62.5 62.5

Example

 Live Demo

M2<-matrix(1:100,nrow=10)
M2

Output

[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 1 11 21 31 41 51 61 71 81 91
[2,] 2 12 22 32 42 52 62 72 82 92
[3,] 3 13 23 33 43 53 63 73 83 93
[4,] 4 14 24 34 44 54 64 74 84 94
[5,] 5 15 25 35 45 55 65 75 85 95
[6,] 6 16 26 36 46 56 66 76 86 96
[7,] 7 17 27 37 47 57 67 77 87 97
[8,] 8 18 28 38 48 58 68 78 88 98
[9,] 9 19 29 39 49 59 69 79 89 99
[10,] 10 20 30 40 50 60 70 80 90 100

Example

apply(M2,1,var)

Output

[1] 916.6667 916.6667 916.6667 916.6667 916.6667 916.6667 916.6667 916.6667 [9] 916.6667 916.6667

Example

 Live Demo

M3<-matrix(1:60,nrow=20)
M3

Output

    [,1] [,2] [,3]
[1,]  1   21 41
[2,]  2   22 42
[3,]  3   23 43
[4,]  4   24 44
[5,]  5   25 45
[6,]  6   26 46
[7,]  7   27 47
[8,]  8   28 48
[9,]  9   29 49
[10,] 10  30 50
[11,] 11  31 51
[12,] 12  32 52
[13,] 13  33 53
[14,] 14  34 54
[15,] 15  35 55
[16,] 16  36 56
[17,] 17  37 57
[18,] 18  38 58
[19,] 19  39 59
[20,] 20  40 60

Example

apply(M3,1,var)

Output

[1] 400 400 400 400 400 400 400 400 400 400 400 400 400 400 400 400 400 400 400
[20] 400

Example

 Live Demo

M4<-matrix(rnorm(30,5,1),nrow=15)
M4

Output

        [,1]    [,2]
[1,] 5.517894  6.105346
[2,] 4.008269  3.640526
[3,] 5.484878  6.779180
[4,] 4.534817  6.723722
[5,] 5.602067  4.032113
[6,] 5.884524  4.910336
[7,] 3.234350  5.824891
[8,] 4.188615  4.874050
[9,] 3.367234  5.062664
[10,] 6.430093  3.369706
[11,] 4.364802  5.902848
[12,] 5.536012  7.037217
[13,] 5.096840 4.269251
[14,] 6.154817 4.320163
[15,] 5.070610 5.150351

Example

apply(M4,1,var)

Output

[1] 0.021906088 0.711543659 0.632177226 0.822552459 0.273495182 1.132347512
[7] 0.058730197 1.369282431 1.130733174 0.625609262 2.226411932 0.081628957
[13] 0.237108400 0.001073659 0.011819879

Example

 Live Demo

M5<-matrix(runif(40,1,2),nrow=20)
M5

Output

[,1] [,2]
[1,] 1.797724 1.045920
[2,] 1.663738 1.404009
[3,] 1.751550 1.920017
[4,] 1.250277 1.445597
[5,] 1.344217 1.975511
[6,] 1.186875 1.877203
[7,] 1.232352 1.912921
[8,] 1.848107 1.016703
[9,] 1.997422 1.888561
[10,] 1.370770 1.548419
[11,] 1.564406 1.925559
[12,] 1.316188 1.024001
[13,] 1.373600 1.642644
[14,] 1.880770 1.861855
[15,] 1.230204 1.628706
[16,] 1.339799 1.782240
[17,] 1.128182 1.186216
[18,] 1.862291 1.140511
[19,] 1.541293 1.454260
[20,] 1.332327 1.398676

Example

apply(M5,1,var)

Output

[1] 3.995889e-05 2.476911e-01 9.689490e-02 3.826634e-02 6.342112e-06
[6] 2.507245e-02 2.410225e-01 7.566494e-02 2.419975e-02 2.205656e-02
[11] 3.307264e-03 1.020207e-01 2.852077e-01 1.436173e-01 4.729889e-03
[16] 4.647355e-02 1.425541e-01 4.943002e-03 4.128207e-02 7.227659e-03

Updated on: 16-Oct-2020

982 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements