Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sorting matrix by column names

Tags:

r

I have the following matrix "z":

             0            1            2            3            4            5            8            9           11           12           15           16           17
[1,] 0.9992149 0.0001345895 4.486317e-05 2.243158e-05 6.729475e-05 8.972633e-05 2.243158e-05 4.486317e-05 4.486317e-05 2.243158e-05 2.243158e-05 6.729475e-05 2.243158e-05
[2,] 0.7116196 0.1991475998 6.440108e-02 1.475998e-02 3.275011e-03 8.075370e-04 0.000000e+00 7.851054e-04 1.345895e-04 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00
[3,] 0.7473531 0.1313817856 2.954240e-02 1.390758e-02 1.195603e-02 1.518618e-02 2.557201e-03 1.615074e-03 1.166442e-03 4.419022e-03 5.383580e-04 4.486317e-04 4.934948e-04
[4,] 0.2177434 0.4941677882 1.351503e-01 3.319874e-02 2.438313e-02 4.311350e-02 6.146254e-03 9.376402e-03 5.607896e-04 4.934948e-04 1.121579e-04 2.018843e-04 4.486317e-05
               18           20           22           24           31            6           10           35           36           37           38          39
[1,] 2.243158e-05 4.486317e-05 2.243158e-05 6.729475e-05 2.243158e-05 0.000000e+00 0.0000000000 0.0000000000 0.000000e+00 0.000000e+00 0.000000e+00 0.000000000
[2,] 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00 6.729475e-05 0.0001794527 0.0001121579 4.486317e-05 6.729475e-05 3.073127e-03 0.001076716
[3,] 6.505159e-04 2.691790e-04 2.691790e-04 8.972633e-05 0.000000e+00 2.433827e-02 0.0029385375 0.0000000000 2.243158e-05 2.243158e-05 2.243158e-05 0.000000000
[4,] 4.486317e-05 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00 2.279049e-02 0.0024450426 0.0000000000 0.000000e+00 0.000000e+00 0.000000e+00 0.000000000
               40           41           42           43           7           13           14           19           21           23           25           26
[1,] 0.0000000000 0.000000e+00 0.000000e+00 0.000000e+00 0.000000000 0.0000000000 0.0000000000 0.000000e+00 0.0000000000 0.000000e+00 0.000000e+00 0.000000e+00
[2,] 0.0003364738 6.729475e-05 2.243158e-05 2.243158e-05 0.000000000 0.0000000000 0.0000000000 0.000000e+00 0.0000000000 0.000000e+00 0.000000e+00 0.000000e+00
[3,] 0.0000000000 0.000000e+00 0.000000e+00 2.243158e-05 0.006976223 0.0018842530 0.0006280843 4.037685e-04 0.0001794527 1.794527e-04 2.467474e-04 4.486317e-05
[4,] 0.0000000000 0.000000e+00 0.000000e+00 0.000000e+00 0.009129655 0.0004486317 0.0003364738 2.243158e-05 0.0000000000 2.243158e-05 4.486317e-05 0.000000e+00
               27           28           30           32           33           34           54           67           45
[1,] 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00
[2,] 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00
[3,] 4.486317e-05 4.486317e-05 2.243158e-05 2.243158e-05 4.486317e-05 2.243158e-05 2.243158e-05 2.243158e-05 0.000000e+00
[4,] 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00 2.243158e-05

their columns names are in fact characters, i.e. "0" and "1". Now I want the column names be integer. So I do:

colnames(z) <- as.integer(colnames(z)

However, I dont know how to sort the matrix by column names, so that the matrix will go from 0,1,2,3,4,5,6,7,8,9,10......

The data structure is below:

structure(c(0.999214894571557, 0.71161956034096, 0.747353073126963, 
0.217743382682817, 0.000134589502018843, 0.199147599820547, 0.13138178555406, 
0.49416778824585, 4.48631673396142e-05, 0.0644010767160162, 0.0295423956931359, 
0.135150291610588, 2.24315836698071e-05, 0.0147599820547331, 
0.0139075818752804, 0.0331987438313145, 6.72947510094213e-05, 
0.00327501121579183, 0.0119560340960072, 0.0243831314490803, 
8.97263346792284e-05, 0.000807537012113055, 0.0151861821444594, 
0.0431135038133692, 2.24315836698071e-05, 0, 0.00255720053835801, 
0.00614625392552714, 4.48631673396142e-05, 0.000785105428443248, 
0.00161507402422611, 0.00937640197397936, 4.48631673396142e-05, 
0.000134589502018843, 0.00116644235082997, 0.000560789591745177, 
2.24315836698071e-05, 0, 0.004419021982952, 0.000493494840735756, 
2.24315836698071e-05, 0, 0.00053835800807537, 0.000112157918349035, 
6.72947510094213e-05, 0, 0.000448631673396142, 0.000201884253028264, 
2.24315836698071e-05, 0, 0.000493494840735756, 4.48631673396142e-05, 
2.24315836698071e-05, 0, 0.000650515926424406, 4.48631673396142e-05, 
4.48631673396142e-05, 0, 0.000269179004037685, 0, 2.24315836698071e-05, 
0, 0.000269179004037685, 0, 6.72947510094213e-05, 0, 8.97263346792284e-05, 
0, 2.24315836698071e-05, 0, 0, 0, 0, 6.72947510094213e-05, 0.0243382682817407, 
0.022790489008524, 0, 0.000179452669358457, 0.00293853746074473, 
0.00244504262000897, 0, 0.000112157918349035, 0, 0, 0, 4.48631673396142e-05, 
2.24315836698071e-05, 0, 0, 6.72947510094213e-05, 2.24315836698071e-05, 
0, 0, 0.00307312696276357, 2.24315836698071e-05, 0, 0, 0.00107671601615074, 
0, 0, 0, 0.000336473755047106, 0, 0, 0, 6.72947510094213e-05, 
0, 0, 0, 2.24315836698071e-05, 0, 0, 0, 2.24315836698071e-05, 
2.24315836698071e-05, 0, 0, 0, 0.00697622252131, 0.00912965455361149, 
0, 0, 0.0018842530282638, 0.000448631673396142, 0, 0, 0.000628084342754598, 
0.000336473755047106, 0, 0, 0.000403768506056528, 2.24315836698071e-05, 
0, 0, 0.000179452669358457, 0, 0, 0, 0.000179452669358457, 2.24315836698071e-05, 
0, 0, 0.000246747420367878, 4.48631673396142e-05, 0, 0, 4.48631673396142e-05, 
0, 0, 0, 4.48631673396142e-05, 0, 0, 0, 4.48631673396142e-05, 
0, 0, 0, 2.24315836698071e-05, 0, 0, 0, 2.24315836698071e-05, 
0, 0, 0, 4.48631673396142e-05, 0, 0, 0, 2.24315836698071e-05, 
0, 0, 0, 2.24315836698071e-05, 0, 0, 0, 2.24315836698071e-05, 
0, 0, 0, 0, 2.24315836698071e-05), .Dim = c(4L, 46L), .Dimnames = list(
    NULL, c("0", "1", "2", "3", "4", "5", "8", "9", "11", "12", 
    "15", "16", "17", "18", "20", "22", "24", "31", "6", "10", 
    "35", "36", "37", "38", "39", "40", "41", "42", "43", "7", 
    "13", "14", "19", "21", "23", "25", "26", "27", "28", "30", 
    "32", "33", "34", "54", "67", "45")))
like image 907
mynameisJEFF Avatar asked Dec 11 '22 12:12

mynameisJEFF


2 Answers

To resort your matrix columns you could use order:

# m is your matrix
m <- m[, order(as.integer(colnames(m)))]

colnames(m)
# [1] "0"  "1"  "2"  "3"  "4"  "5"  "6"  "7"  "8"  "9"  "10" "11" "12" "13" "14" "15" "16" "17" "18" "19" "20" "21" "22" "23" "24" "25" "26" "27" "28" "30" "31" "32" "33"
# [34] "34" "35" "36" "37" "38" "39" "40" "41" "42" "43" "45" "54" "67"
like image 64
sgibb Avatar answered Jan 02 '23 22:01

sgibb


Try mixedsort from gtools...

require(gtools)
mixedsort( colnames( z ) )
#[1] "0"  "1"  "2"  "3"  "4"  "5"  "6"  "7"  "8"  "9"  "10" "11" "12" "13" "14" "15" "16" "17" "18" "19" "20" "21" "22" "23" "24" "25" "26" "27" "28" "30" "31"
#[32] "32" "33" "34" "35" "36" "37" "38" "39" "40" "41" "42" "43" "45" "54" "67"

As an aside you cannot have integer colnames. It is not syntactically valid. From ?colnames...

...for colnames a character vector of (preferably) unique syntactically-valid names. In both cases, value will be coerced by as.character, and setting colnames will convert the row names to character.

like image 41
Simon O'Hanlon Avatar answered Jan 02 '23 20:01

Simon O'Hanlon