Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Sort a matrix by descending value for every row in order

I have a matrix which I want ordered by every column. Probably better to show the before and after. This is the matrix before.

thematrix_before <- structure(c(0, 0, 0, 0, 0, 0.44, 0.68, 0, 0, 0, 0, 0, 0, 0, 0, 
            0, 0, 0, 0.42, 0.39, 0, 0, 0, 0, 0, 0.35, 0, 0, 0, 0, 0, 0, 0, 
            0, 0, 0, 0, 0.55, 0, 0, 0, 0, 0, 0, 0.54, 0, 0, 0, 0, 0, 0, 0, 
            0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.41, 0.73, 0, 0, 0, 
            0, 0, 0, 0, 0, 0, 0.44, 0.69, 0, 0, 0, 0, 0, 0.45, 0.67, 0, 0, 
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), .Dim = c(15L, 8L), .Dimnames = list(
              c("rQ1_10", "rQ1_11", "rQ1_13", "rQ1_14", "rQ1_15", "rQ1_16", 
                "rQ1_17", "rQ1_2", "rQ1_23", "rQ1_24", "rQ1_29", "rQ1_3", 
                "rQ1_4", "rQ1_5", "rQ1_1"), c("Features", "Innovation", 
                                                      "KPI", "Knowledge", "Performance", "Presence", "Trust", "VFM"
                )))

    > thematrix_before 
           Features Innovation  KPI Knowledge Performance Presence Trust  VFM
    rQ1_10     0.00       0.00 0.00         0           0     0.00  0.00 0.45
    rQ1_11     0.00       0.00 0.00         0           0     0.00  0.00 0.67
    rQ1_13     0.00       0.00 0.00         0           1     0.00  0.00 0.00
    rQ1_14     0.00       0.42 0.00         0           0     0.00  0.00 0.00
    rQ1_15     0.00       0.39 0.00         0           0     0.00  0.00 0.00
    rQ1_16     0.44       0.00 0.00         0           0     0.00  0.00 0.00
    rQ1_17     0.68       0.00 0.00         0           0     0.00  0.00 0.00
    rQ1_2      0.00       0.00 0.55         0           0     0.00  0.00 0.00
    rQ1_23     0.00       0.00 0.00         0           0     0.00  0.44 0.00
    rQ1_24     0.00       0.00 0.00         0           0     0.00  0.69 0.00
    rQ1_29     0.00       0.35 0.00         0           0     0.00  0.00 0.00
    rQ1_3      0.00       0.00 0.00         1           0     0.00  0.00 0.00
    rQ1_4      0.00       0.00 0.00         0           0     0.41  0.00 0.00
    rQ1_5      0.00       0.00 0.00         0           0     0.73  0.00 0.00
    rQ1_1      0.00       0.00 0.54         0           0     0.00  0.00 0.00

Below is how the matrix would look. Apologies, if anything is out of place, as I edited this manually to give you an idea of how it would look. It will sort the "Features" column first, then the next column, then the next, through all columns till it ends up like this. The columns are in same position, and the rows have been reorganised.

> thematrix_reordered
       Features Innovation  KPI Knowledge Performance Presence Trust  VFM
rQ1_17     0.68       0.00 0.00         0           0     0.00  0.00 0.00
rQ1_16     0.44       0.00 0.00         0           0     0.00  0.00 0.00
rQ1_14     0.00       0.42 0.00         0           0     0.00  0.00 0.00
rQ1_15     0.00       0.39 0.00         0           0     0.00  0.00 0.00
rQ1_29     0.00       0.35 0.00         0           0     0.00  0.00 0.00
rQ1_2      0.00       0.00 0.55         0           0     0.00  0.00 0.00
rQ1_1      0.00       0.00 0.54         0           0     0.00  0.00 0.00
rQ1_3      0.00       0.00 0.00         1           0     0.00  0.00 0.00
rQ1_13     0.00       0.00 0.00         0           1     0.00  0.00 0.00
rQ1_5      0.00       0.00 0.00         0           0     0.73  0.00 0.00
rQ1_4      0.00       0.00 0.00         0           0     0.41  0.00 0.00
rQ1_24     0.00       0.00 0.00         0           0     0.00  0.69 0.00
rQ1_23     0.00       0.00 0.00         0           0     0.00  0.44 0.00
rQ1_11     0.00       0.00 0.00         0           0     0.00  0.00 0.67
rQ1_10     0.00       0.00 0.00         0           0     0.00  0.00 0.45

I’m going to use this code for multiple different matrixes. Therefore, if possible need a code that is replicable for any matrix, regardless of name of columns. e.g. DF[ order by all names(DF), ].

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Using the order function. Watch the commas!

thematrix_before[order(thematrix_before[, 'Features'], thematrix_before[, 'Innovation'], thematrix_before[, 'KPI'], thematrix_before[, 'Knowledge'], thematrix_before[, 'Performance'], thematrix_before[, 'Presence'], thematrix_before[, 'Trust'], thematrix_before[, 'VFM'], decreasing=TRUE), ]
#        Features Innovation  KPI Knowledge Performance Presence Trust  VFM
# rQ1_17     0.68       0.00 0.00         0           0     0.00  0.00 0.00
# rQ1_16     0.44       0.00 0.00         0           0     0.00  0.00 0.00
# rQ1_14     0.00       0.42 0.00         0           0     0.00  0.00 0.00
# rQ1_15     0.00       0.39 0.00         0           0     0.00  0.00 0.00
# rQ1_29     0.00       0.35 0.00         0           0     0.00  0.00 0.00
# rQ1_2      0.00       0.00 0.55         0           0     0.00  0.00 0.00
# rQ1_1      0.00       0.00 0.54         0           0     0.00  0.00 0.00
# rQ1_3      0.00       0.00 0.00         1           0     0.00  0.00 0.00
# rQ1_13     0.00       0.00 0.00         0           1     0.00  0.00 0.00
# rQ1_5      0.00       0.00 0.00         0           0     0.73  0.00 0.00
# rQ1_4      0.00       0.00 0.00         0           0     0.41  0.00 0.00
# rQ1_24     0.00       0.00 0.00         0           0     0.00  0.69 0.00
# rQ1_23     0.00       0.00 0.00         0           0     0.00  0.44 0.00
# rQ1_11     0.00       0.00 0.00         0           0     0.00  0.00 0.67
# rQ1_10     0.00       0.00 0.00         0           0     0.00  0.00 0.45

For any matrix

thematrix_before[do.call('order', c(data.frame(thematrix_before), decreasing=TRUE)), ]
#        Features Innovation  KPI Knowledge Performance Presence Trust  VFM
# rQ1_17     0.68       0.00 0.00         0           0     0.00  0.00 0.00
# rQ1_16     0.44       0.00 0.00         0           0     0.00  0.00 0.00
# rQ1_14     0.00       0.42 0.00         0           0     0.00  0.00 0.00
# rQ1_15     0.00       0.39 0.00         0           0     0.00  0.00 0.00
# rQ1_29     0.00       0.35 0.00         0           0     0.00  0.00 0.00
# rQ1_2      0.00       0.00 0.55         0           0     0.00  0.00 0.00
# rQ1_1      0.00       0.00 0.54         0           0     0.00  0.00 0.00
# rQ1_3      0.00       0.00 0.00         1           0     0.00  0.00 0.00
# rQ1_13     0.00       0.00 0.00         0           1     0.00  0.00 0.00
# rQ1_5      0.00       0.00 0.00         0           0     0.73  0.00 0.00
# rQ1_4      0.00       0.00 0.00         0           0     0.41  0.00 0.00
# rQ1_24     0.00       0.00 0.00         0           0     0.00  0.69 0.00
# rQ1_23     0.00       0.00 0.00         0           0     0.00  0.44 0.00
# rQ1_11     0.00       0.00 0.00         0           0     0.00  0.00 0.67
# rQ1_10     0.00       0.00 0.00         0           0     0.00  0.00 0.45
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading