How to sort matrix columns independently in increasing order in R?


To sort a matrix column independently in increasing order means that ordering each column of the data frame in increasing order, therefore, sorting of values in one column does not affect the sorting in other columns. This can be done with the help of apply function along with the sort function as shown in the below examples.

Example

 Live Demo

M1<-matrix(rpois(80,5),ncol=4)
M1

Output

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

apply(M1,2,sort)

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

Example

 Live Demo

M2<-matrix(rnorm(45),ncol=3)
M2

Output

        [,1]      [,2]         [,3]
[1,]  0.7035341   0.1684070   0.6266624
[2,]  0.9411680   0.3888510  -0.6253838
[3,] -1.5572345  0.2111508   1.6147692
[4,]  0.9861180   0.3051785   0.4764457
[5,]  0.4121853   -0.2924738  -0.6538284
[6,] -0.2941798   1.4971932   -1.0346647
[7,] -0.8095073  0.2299736    0.9276157
[8,]  0.6784916   -0.3066246   -2.1540382
[9,]  0.3752442   -0.8025764   -0.8814537
[10,] 0.5712123  1.4410942    -0.8682303
[11,] 0.8312513   -1.0763055  1.8776386
[12,] -0.8845890  -0.3053165   1.1853445
[13,] -1.2043000  0.3795757    0.1601545
[14,] -0.8973574   -2.6955627  1.4023479
[15,] -0.7261224   2.0387155   1.0077507

apply(M2,2,sort)

           [,1]    [,2]         [,3]
[1,]  -1.5572345   -2.6955627   -2.1540382
[2,]  -1.2043000   -1.0763055   -1.0346647
[3,]  -0.8973574   -0.8025764   -0.8814537
[4,]  -0.8845890   -0.3066246   -0.8682303
[5,]  -0.8095073   -0.3053165   -0.6538284
[6,]  -0.7261224   -0.2924738   -0.6253838
[7,]  -0.2941798   0.1684070    0.1601545
[8,]  0.3752442    0.2111508    0.4764457
[9,]  0.4121853   0.2299736    0.6266624
[10,]  0.5712123  0.3051785    0.9276157
[11,]  0.6784916   0.3795757   1.0077507
[12,]  0.7035341   0.3888510   1.1853445
[13,]  0.8312513   1.4410942   1.4023479
[14,]  0.9411680   1.4971932   1.6147692
[15,]  0.9861180   2.0387155   1.8776386

Example

 Live Demo

M3<-matrix(runif(40,2,10),ncol=2)
M3

Output

         [,1]    [,2]
[1,] 8.265500   3.812385
[2,] 8.213926   9.066299
[3,] 9.004847   5.589592
[4,] 6.364649   6.756629
[5,] 4.953512   3.778917
[6,] 8.354575   6.133739
[7,] 6.494883   7.320632
[8,] 2.769262   5.570083
[9,] 4.301743   6.874214
[10,] 5.467977  5.934101
[11,] 7.847425  9.373401
[12,] 7.860578 9.059702
[13,] 6.378894 3.864647
[14,] 7.988057 7.527622
[15,] 3.395857 2.156028
[16,] 3.060717 9.413530
[17,] 9.574119 2.519779
[18,] 9.705956 2.428624
[19,] 3.753683 6.364985
[20,] 9.969481 8.733604

apply(M3,2,sort)

[,1] [,2]
[1,] 2.769262 2.156028
[2,] 3.060717 2.428624
[3,] 3.395857 2.519779
[4,] 3.753683 3.778917
[5,] 4.301743 3.812385
[6,] 4.953512 3.864647
[7,] 5.467977 5.570083
[8,] 6.364649 5.589592
[9,] 6.378894 5.934101
[10,] 6.494883 6.133739
[11,] 7.847425 6.364985
[12,] 7.860578 6.756629
[13,] 7.988057 6.874214
[14,] 8.213926 7.320632
[15,] 8.265500 7.527622
[16,] 8.354575 8.733604
[17,] 9.004847 9.059702
[18,] 9.574119 9.066299
[19,] 9.705956 9.373401
[20,] 9.969481 9.413530

Updated on: 06-Feb-2021

271 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements