我有一个40行的矩阵。看起来像这样:
Sample_10.rare 0 0
Sample_11.rare 0 42
Sample_12.rare 0 0
Sample_13.rare 762 550
Sample_14.rare 847 508
Sample_15.rare 1143 593
Sample_16.rare 5630 5291
Sample_17.rare 4064 3683
Sample_18.rare 5969 4318
Sample_19.rare 42 212
Sample_1.rare 5037 3598
Sample_20.rare 127 381
Sample_21.rare 127 0
Sample_22.rare 339 254
Sample_23.rare 0 85
Sample_24.rare 0 0
Sample_25.rare 42 42
Sample_26.rare 339 339
Sample_27.rare 5376 4741
Sample_28.rare 11006 8678
Sample_29.rare 339 296
Sample_2.rare 6815 5164
Sample_30.rare 212 466
我想重新排列行的顺序。从第Sample_1.rare
行到第Sample_40.rare
行,顺序良好。有解决办法吗?
答案 0 :(得分:1)
尝试使用mixedorder
软件包中的gtools
,即
m2[gtools::mixedorder(rownames(m2)),]
给出,
V2 V3 Sample_1.rare 5037 3598 Sample_2.rare 6815 5164 Sample_10.rare 0 0 Sample_11.rare 0 42 Sample_12.rare 0 0 Sample_13.rare 762 550 Sample_14.rare 847 508 Sample_15.rare 1143 593 Sample_16.rare 5630 5291 Sample_17.rare 4064 3683 Sample_18.rare 5969 4318 Sample_19.rare 42 212 Sample_20.rare 127 381 Sample_21.rare 127 0 Sample_22.rare 339 254 Sample_23.rare 0 85 Sample_24.rare 0 0 Sample_25.rare 42 42 Sample_26.rare 339 339 Sample_27.rare 5376 4741 Sample_28.rare 11006 8678 Sample_29.rare 339 296 Sample_30.rare 212 466
数据
dput(m2)
structure(c(0L, 0L, 0L, 762L, 847L, 1143L, 5630L, 4064L, 5969L,
42L, 5037L, 127L, 127L, 339L, 0L, 0L, 42L, 339L, 5376L, 11006L,
339L, 6815L, 212L, 0L, 42L, 0L, 550L, 508L, 593L, 5291L, 3683L,
4318L, 212L, 3598L, 381L, 0L, 254L, 85L, 0L, 42L, 339L, 4741L,
8678L, 296L, 5164L, 466L), .Dim = c(23L, 2L), .Dimnames = list(
c("Sample_10.rare", "Sample_11.rare", "Sample_12.rare", "Sample_13.rare",
"Sample_14.rare", "Sample_15.rare", "Sample_16.rare", "Sample_17.rare",
"Sample_18.rare", "Sample_19.rare", "Sample_1.rare", "Sample_20.rare",
"Sample_21.rare", "Sample_22.rare", "Sample_23.rare", "Sample_24.rare",
"Sample_25.rare", "Sample_26.rare", "Sample_27.rare", "Sample_28.rare",
"Sample_29.rare", "Sample_2.rare", "Sample_30.rare"), c("V2",
"V3")))