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

Reorder matrix rows and columns based on alphabetical order of colnames and rownames in R

I was wondering if there is a way to reorder the rows and columns of the MATRIX below by their rownames and colnames in alphabetical order?

MATRIX <- structure(c(0.00096, 0.00047, -0.00027, -0.00018, 4e-04, 0.00022, 
0.00047, 0.00105, -3e-04, -8e-05, 0.00018, 0.00041, -0.00027, 
-3e-04, 0.00071, 3e-05, -4e-05, -8e-05, -0.00018, -8e-05, 3e-05, 
0.00066, -0.00023, -0.00024, 4e-04, 0.00018, -4e-05, -0.00023, 
0.00083, 0.00036, 0.00022, 0.00041, -8e-05, -0.00024, 0.00036, 
0.00092), dim = c(6L, 6L), dimnames = list(c("acog.perf", "asom.perf", 
"conf.perf", "acog.asom", "acog.conf", "asom.conf"), c("acog.perf", 
"asom.perf", "conf.perf", "acog.asom", "acog.conf", "asom.conf"
)))

>Solution :

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

You can sort the rownames() and colnames() with sort() and use them
to index MATRIX via the square bracket indexing [x, y], where x indexes
rows and y columns.

MATRIX[sort(rownames(MATRIX)), sort(colnames(MATRIX))]
#>           acog.asom acog.conf acog.perf asom.conf asom.perf conf.perf
#> acog.asom   0.00066  -0.00023  -0.00018  -0.00024  -0.00008   0.00003
#> acog.conf  -0.00023   0.00083   0.00040   0.00036   0.00018  -0.00004
#> acog.perf  -0.00018   0.00040   0.00096   0.00022   0.00047  -0.00027
#> asom.conf  -0.00024   0.00036   0.00022   0.00092   0.00041  -0.00008
#> asom.perf  -0.00008   0.00018   0.00047   0.00041   0.00105  -0.00030
#> conf.perf   0.00003  -0.00004  -0.00027  -0.00008  -0.00030   0.00071
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