How to change the order of a matrix in increasing order based on a single column?

R ProgrammingServer Side ProgrammingProgramming

To change the order of a matrix in increasing order based on a single column in R, we can use order function after subsetting a particular column.

For example, if we have a matrix called M and we want to change the order of M in increasing order based on first column then it can be done by using the following command −

M[order(M[,1]),]

Example 1

Following snippet creates a matrix −

M1<-matrix(rpois(100,10),ncol=5)
M1

The following matrix is created −

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

To change the order of M1 in increasing order based on column 1, add the following code to the above snippet −

M1<-matrix(rpois(100,10),ncol=5)
M1[order(M1[,1]),]

Output

If you execute all the above given snippets as a single program, it generates the following output −

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

Example 2

Following snippet creates a matrix −

M2<-matrix(round(rnorm(80),2),ncol=4)
M2

The following matrix is created −

       [,1]  [,2]  [,3]  [,4]
[1,]   2.09  0.99  1.01  -2.08
[2,]  -0.68 -1.94 -0.52  -0.09
[3,]  -1.86  0.11 -0.29   1.19
[4,]   0.53  0.61  0.40   1.19
[5,]   0.31 -1.45 -0.55  -0.79
[6,]  -1.35  0.48  0.09  -1.55
[7,]  -1.94 -0.83 -1.96   2.46
[8,]  -0.12  1.02 -1.12  -0.16
[9,]   1.14  0.54 -1.33  -0.10
[10,]  0.64  0.77 -0.85   0.42
[11,] -0.49  0.12 -0.69  -1.61
[12,] -0.83  0.86  0.38  -0.73
[13,]  0.27  1.38  0.98  -1.54
[14,]  0.16  1.97 -0.73  -0.69
[15,]  0.63 -0.03 -1.00   0.12
[16,] -0.40 -2.25 -1.04  -1.36
[17,]  0.90  0.03 -0.41   0.59
[18,] -0.83  0.21 -0.24   0.29
[19,] -0.33 -0.16  0.48  -0.90
[20,]  0.74  0.57 -0.32   0.23

To change the order of M2 in increasing order based on column 4, add the following code to the above snippet −

M2<-matrix(round(rnorm(80),2),ncol=4)
M2[order(M2[,4]),]

Output

If you execute all the above given snippets as a single program, it generates the following output −

       [,1]  [,2]  [,3]   [,4]
[1,]   2.09  0.99  1.01 -2.08
[2,]  -0.49  0.12 -0.69 -1.61
[3,]  -1.35  0.48  0.09 -1.55
[4,]   0.27  1.38  0.98 -1.54
[5,]  -0.40 -2.25 -1.04 -1.36
[6,]  -0.33 -0.16  0.48 -0.90
[7,]   0.31 -1.45 -0.55 -0.79
[8,]  -0.83  0.86  0.38 -0.73
[9,]   0.16  1.97 -0.73 -0.69
[10,] -0.12  1.02 -1.12 -0.16
[11,]  1.14  0.54 -1.33 -0.10
[12,] -0.68 -1.94 -0.52 -0.09
[13,]  0.63 -0.03 -1.00  0.12
[14,]  0.74  0.57 -0.32  0.23
[15,] -0.83  0.21 -0.24  0.29
[16,]  0.64  0.77 -0.85  0.42
[17,]  0.90  0.03 -0.41  0.59
[18,] -1.86  0.11 -0.29  1.19
[19,]  0.53  0.61  0.40  1.19
[20,] -1.94 -0.83 -1.96  2.46
raja
Updated on 23-Nov-2021 05:40:26

Advertisements