rmatrixterrar-colnamesr-rownames

Ordering rows and columns of R Matrix


I have a matrix in R like this:

bio1 bio10 bio11 bio12 bio13 bio14 bio15 bio16 bio17 bio18 bio19  bio2  bio3  bio4  bio5  bio6  bio7  bio8  bio9
bio1   1.00  0.90  0.91 -0.40 -0.21 -0.54  0.71 -0.22 -0.54 -0.57  0.02  0.45  0.45 -0.13  0.84  0.85  0.05 -0.07  0.73
bio10  0.90  1.00  0.64 -0.60 -0.44 -0.64  0.57 -0.45 -0.65 -0.68 -0.19  0.48  0.19  0.32  0.97  0.56  0.44  0.02  0.71
bio11  0.91  0.64  1.00 -0.13  0.04 -0.35  0.71  0.04 -0.35 -0.38  0.23  0.32  0.58 -0.52  0.57  0.98 -0.33 -0.15  0.62
bio12 -0.40 -0.60 -0.13  1.00  0.93  0.74 -0.23  0.94  0.76  0.71  0.78 -0.31  0.07 -0.51 -0.60 -0.04 -0.55 -0.08 -0.40
bio13 -0.21 -0.44  0.04  0.93  1.00  0.48  0.06  1.00  0.50  0.51  0.87 -0.19  0.19 -0.55 -0.43  0.11 -0.53 -0.17 -0.22
bio14 -0.54 -0.64 -0.35  0.74  0.48  1.00 -0.64  0.50  0.99  0.90  0.27 -0.44 -0.17 -0.28 -0.67 -0.25 -0.43  0.27 -0.65
bio15  0.71  0.57  0.71 -0.23  0.06 -0.64  1.00  0.05 -0.64 -0.56  0.21  0.64  0.67 -0.25  0.62  0.60  0.06 -0.45  0.68
bio16 -0.22 -0.45  0.04  0.94  1.00  0.50  0.05  1.00  0.52  0.52  0.88 -0.19  0.19 -0.54 -0.44  0.11 -0.53 -0.17 -0.23
bio17 -0.54 -0.65 -0.35  0.76  0.50  0.99 -0.64  0.52  1.00  0.90  0.29 -0.46 -0.18 -0.29 -0.69 -0.25 -0.45  0.27 -0.65
bio18 -0.57 -0.68 -0.38  0.71  0.51  0.90 -0.56  0.52  0.90  1.00  0.18 -0.43 -0.16 -0.29 -0.71 -0.29 -0.44  0.38 -0.75
bio19  0.02 -0.19  0.23  0.78  0.87  0.27  0.21  0.88  0.29  0.18  1.00 -0.02  0.31 -0.50 -0.17  0.28 -0.42 -0.36  0.07
bio2   0.45  0.48  0.32 -0.31 -0.19 -0.44  0.64 -0.19 -0.46 -0.43 -0.02  1.00  0.76  0.13  0.66  0.13  0.53 -0.33  0.47
bio3   0.45  0.19  0.58  0.07  0.19 -0.17  0.67  0.19 -0.18 -0.16  0.31  0.76  1.00 -0.52  0.31  0.47 -0.13 -0.37  0.36
bio4  -0.13  0.32 -0.52 -0.51 -0.55 -0.28 -0.25 -0.54 -0.29 -0.29 -0.50  0.13 -0.52  1.00  0.37 -0.58  0.90  0.21  0.02
bio5   0.84  0.97  0.57 -0.60 -0.43 -0.67  0.62 -0.44 -0.69 -0.71 -0.17  0.66  0.31  0.37  1.00  0.45  0.57 -0.08  0.72
bio6   0.85  0.56  0.98 -0.04  0.11 -0.25  0.60  0.11 -0.25 -0.29  0.28  0.13  0.47 -0.58  0.45  1.00 -0.47 -0.08  0.54
bio7   0.05  0.44 -0.33 -0.55 -0.53 -0.43  0.06 -0.53 -0.45 -0.44 -0.42  0.53 -0.13  0.90  0.57 -0.47  1.00  0.00  0.22
bio8  -0.07  0.02 -0.15 -0.08 -0.17  0.27 -0.45 -0.17  0.27  0.38 -0.36 -0.33 -0.37  0.21 -0.08 -0.08  0.00  1.00 -0.48
bio9   0.73  0.71  0.62 -0.40 -0.22 -0.65  0.68 -0.23 -0.65 -0.75  0.07  0.47  0.36  0.02  0.72  0.54  0.22 -0.48  1.00
correlation_bio <- `structure(c(1, 0.9, 0.91, -0.4, -0.21, -0.54, 0.71, -0.22, -0.54,  -0.57, 0.02, 0.45, 0.45, -0.13, 0.84, 0.85, 0.05, -0.07, 0.73,  0.9, 1, 0.64, -0.6, -0.44, -0.64, 0.57, -0.45, -0.65, -0.68,  -0.19, 0.48, 0.19, 0.32, 0.97, 0.56, 0.44, 0.02, 0.71, 0.91,  0.64, 1, -0.13, 0.04, -0.35, 0.71, 0.04, -0.35, -0.38, 0.23,  0.32, 0.58, -0.52, 0.57, 0.98, -0.33, -0.15, 0.62, -0.4, -0.6,  -0.13, 1, 0.93, 0.74, -0.23, 0.94, 0.76, 0.71, 0.78, -0.31, 0.07,  -0.51, -0.6, -0.04, -0.55, -0.08, -0.4, -0.21, -0.44, 0.04, 0.93,  1, 0.48, 0.06, 1, 0.5, 0.51, 0.87, -0.19, 0.19, -0.55, -0.43,  0.11, -0.53, -0.17, -0.22, -0.54, -0.64, -0.35, 0.74, 0.48, 1,  -0.64, 0.5, 0.99, 0.9, 0.27, -0.44, -0.17, -0.28, -0.67, -0.25,  -0.43, 0.27, -0.65, 0.71, 0.57, 0.71, -0.23, 0.06, -0.64, 1,  0.05, -0.64, -0.56, 0.21, 0.64, 0.67, -0.25, 0.62, 0.6, 0.06,  -0.45, 0.68, -0.22, -0.45, 0.04, 0.94, 1, 0.5, 0.05, 1, 0.52,  0.52, 0.88, -0.19, 0.19, -0.54, -0.44, 0.11, -0.53, -0.17, -0.23,  -0.54, -0.65, -0.35, 0.76, 0.5, 0.99, -0.64, 0.52, 1, 0.9, 0.29,  -0.46, -0.18, -0.29, -0.69, -0.25, -0.45, 0.27, -0.65, -0.57,  -0.68, -0.38, 0.71, 0.51, 0.9, -0.56, 0.52, 0.9, 1, 0.18, -0.43,  -0.16, -0.29, -0.71, -0.29, -0.44, 0.38, -0.75, 0.02, -0.19,  0.23, 0.78, 0.87, 0.27, 0.21, 0.88, 0.29, 0.18, 1, -0.02, 0.31,  -0.5, -0.17, 0.28, -0.42, -0.36, 0.07, 0.45, 0.48, 0.32, -0.31,  -0.19, -0.44, 0.64, -0.19, -0.46, -0.43, -0.02, 1, 0.76, 0.13,  0.66, 0.13, 0.53, -0.33, 0.47, 0.45, 0.19, 0.58, 0.07, 0.19,  -0.17, 0.67, 0.19, -0.18, -0.16, 0.31, 0.76, 1, -0.52, 0.31,  0.47, -0.13, -0.37, 0.36, -0.13, 0.32, -0.52, -0.51, -0.55, -0.28,  -0.25, -0.54, -0.29, -0.29, -0.5, 0.13, -0.52, 1, 0.37, -0.58,  0.9, 0.21, 0.02, 0.84, 0.97, 0.57, -0.6, -0.43, -0.67, 0.62,  -0.44, -0.69, -0.71, -0.17, 0.66, 0.31, 0.37, 1, 0.45, 0.57,  -0.08, 0.72, 0.85, 0.56, 0.98, -0.04, 0.11, -0.25, 0.6, 0.11,  -0.25, -0.29, 0.28, 0.13, 0.47, -0.58, 0.45, 1, -0.47, -0.08,  0.54, 0.05, 0.44, -0.33, -0.55, -0.53, -0.43, 0.06, -0.53, -0.45,  -0.44, -0.42, 0.53, -0.13, 0.9, 0.57, -0.47, 1, 0, 0.22, -0.07,  0.02, -0.15, -0.08, -0.17, 0.27, -0.45, -0.17, 0.27, 0.38, -0.36,  -0.33, -0.37, 0.21, -0.08, -0.08, 0, 1, -0.48, 0.73, 0.71, 0.62,  -0.4, -0.22, -0.65, 0.68, -0.23, -0.65, -0.75, 0.07, 0.47, 0.36,  0.02, 0.72, 0.54, 0.22, -0.48, 1), dim = c(19L, 19L), dimnames = list(     c("bio1", "bio10", "bio11", "bio12", "bio13", "bio14", "bio15",      "bio16", "bio17", "bio18", "bio19", "bio2", "bio3", "bio4",      "bio5", "bio6", "bio7", "bio8", "bio9"), c("bio1", "bio10",      "bio11", "bio12", "bio13", "bio14", "bio15", "bio16", "bio17",      "bio18", "bio19", "bio2", "bio3", "bio4", "bio5", "bio6",      "bio7", "bio8", "bio9")))`

As you can see, both rows and columns start with "bio 1", but then continue with "bio10" instead of "bio2".

Both the row and the column should be sorted like this; "bio1", "bio2", "bio3", "bio4", "bio5", "bio6", "bio7", "bio8", "bio9", "bio10", "bio11", "bio12", "bio13" ", "bio14", "bio15", "bio16", "bio17", "bio18", "bio19".

How can I fix this?


Solution

  • In base R you can do

    ind <- order(as.numeric(sub('bio', '', row.names(correlation_bio))))
    correlation_bio <- correlation_bio[ind, ind]
    

    Resulting in

    correlation_bio
    #>        bio1  bio2  bio3  bio4  bio5  bio6  bio7  bio8  bio9 bio10 bio11 bio12 bio13 bio14 bio15 bio16 bio17 bio18 bio19
    #> bio1   1.00  0.45  0.45 -0.13  0.84  0.85  0.05 -0.07  0.73  0.90  0.91 -0.40 -0.21 -0.54  0.71 -0.22 -0.54 -0.57  0.02
    #> bio2   0.45  1.00  0.76  0.13  0.66  0.13  0.53 -0.33  0.47  0.48  0.32 -0.31 -0.19 -0.44  0.64 -0.19 -0.46 -0.43 -0.02
    #> bio3   0.45  0.76  1.00 -0.52  0.31  0.47 -0.13 -0.37  0.36  0.19  0.58  0.07  0.19 -0.17  0.67  0.19 -0.18 -0.16  0.31
    #> bio4  -0.13  0.13 -0.52  1.00  0.37 -0.58  0.90  0.21  0.02  0.32 -0.52 -0.51 -0.55 -0.28 -0.25 -0.54 -0.29 -0.29 -0.50
    #> bio5   0.84  0.66  0.31  0.37  1.00  0.45  0.57 -0.08  0.72  0.97  0.57 -0.60 -0.43 -0.67  0.62 -0.44 -0.69 -0.71 -0.17
    #> bio6   0.85  0.13  0.47 -0.58  0.45  1.00 -0.47 -0.08  0.54  0.56  0.98 -0.04  0.11 -0.25  0.60  0.11 -0.25 -0.29  0.28
    #> bio7   0.05  0.53 -0.13  0.90  0.57 -0.47  1.00  0.00  0.22  0.44 -0.33 -0.55 -0.53 -0.43  0.06 -0.53 -0.45 -0.44 -0.42
    #> bio8  -0.07 -0.33 -0.37  0.21 -0.08 -0.08  0.00  1.00 -0.48  0.02 -0.15 -0.08 -0.17  0.27 -0.45 -0.17  0.27  0.38 -0.36
    #> bio9   0.73  0.47  0.36  0.02  0.72  0.54  0.22 -0.48  1.00  0.71  0.62 -0.40 -0.22 -0.65  0.68 -0.23 -0.65 -0.75  0.07
    #> bio10  0.90  0.48  0.19  0.32  0.97  0.56  0.44  0.02  0.71  1.00  0.64 -0.60 -0.44 -0.64  0.57 -0.45 -0.65 -0.68 -0.19
    #> bio11  0.91  0.32  0.58 -0.52  0.57  0.98 -0.33 -0.15  0.62  0.64  1.00 -0.13  0.04 -0.35  0.71  0.04 -0.35 -0.38  0.23
    #> bio12 -0.40 -0.31  0.07 -0.51 -0.60 -0.04 -0.55 -0.08 -0.40 -0.60 -0.13  1.00  0.93  0.74 -0.23  0.94  0.76  0.71  0.78
    #> bio13 -0.21 -0.19  0.19 -0.55 -0.43  0.11 -0.53 -0.17 -0.22 -0.44  0.04  0.93  1.00  0.48  0.06  1.00  0.50  0.51  0.87
    #> bio14 -0.54 -0.44 -0.17 -0.28 -0.67 -0.25 -0.43  0.27 -0.65 -0.64 -0.35  0.74  0.48  1.00 -0.64  0.50  0.99  0.90  0.27
    #> bio15  0.71  0.64  0.67 -0.25  0.62  0.60  0.06 -0.45  0.68  0.57  0.71 -0.23  0.06 -0.64  1.00  0.05 -0.64 -0.56  0.21
    #> bio16 -0.22 -0.19  0.19 -0.54 -0.44  0.11 -0.53 -0.17 -0.23 -0.45  0.04  0.94  1.00  0.50  0.05  1.00  0.52  0.52  0.88
    #> bio17 -0.54 -0.46 -0.18 -0.29 -0.69 -0.25 -0.45  0.27 -0.65 -0.65 -0.35  0.76  0.50  0.99 -0.64  0.52  1.00  0.90  0.29
    #> bio18 -0.57 -0.43 -0.16 -0.29 -0.71 -0.29 -0.44  0.38 -0.75 -0.68 -0.38  0.71  0.51  0.90 -0.56  0.52  0.90  1.00  0.18
    #> bio19  0.02 -0.02  0.31 -0.50 -0.17  0.28 -0.42 -0.36  0.07 -0.19  0.23  0.78  0.87  0.27  0.21  0.88  0.29  0.18  1.00