按升序对列名称进行排序 - 字符串后跟数字

时间:2018-02-08 09:41:22

标签: r

我想按升序对数据框中的列进行排序/排序。但是,问题是数字前面是一个字符串。

structure(list(H = c("P01050.1", "P01080.1", 
                                 "P01090.1"), Gr_1 = c(0, 1107200, 17096000), Gr_10 = c(0, 
                                                                                                               37259000, 1104800000), Gr_11 = c(1835800, 53909000, 
                                                                                                                                                          623960000), Gr_12 = c(0, 19117000, 808600000), Gr_13 = c(2544200, 
                                                                                                                                                                                                                                       2461400, 418770000), Gr_14 = c(5120400, 1373700, 117330000
                                                                                                                                                                                                                                       ), Gr_15 = c(6623500, 0, 73336000), Gr_16 = c(0, 
                                                                                                                                                                                                                                                                                                         0, 31761000), Gr_17 = c(13475000, 0, 29387000), Gr_18 = c(7883300, 
                                                                                                                                                                                                                                                                                                                                                                                       0, 27476000), Gr_19 = c(82339000, 3254700, 50825000
                                                                                                                                                                                                                                                                                                                                                                                       ), Gr_2 = c(1584100, 84847000, 5219500000), Gr_20 = c(205860000, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                 0, 67685000), Gr_21 = c(867120000, 1984400, 2.26e+08
                                                                                                                                                                                                                                                                                                                                                                                                                                                                 ), Gr_22 = c(1144300000, 11342000, 256440000), Gr_23 = c(1.179e+09, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              4391600, 141230000), Gr_24 = c(1408700000, 2830100, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       328190000), Gr_25 = c(757020000, 5588500, 113360000
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       ), Gr_26 = c(456930000, 4694700, 78221000), Gr_27 = c(162310000, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 12425000, 29132000), Gr_28 = c(71199000, 13883000, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          39425000), Gr_29 = c(220140000, 48225000, 22240000), 
               Gr_3 = c(4206000, 291080000, 1.5496e+10), Gr_30 = c(418310000, 
                                                                                       248370000, 2998900), Gr_31 = c(214340000, 342310000, 
                                                                                                                                6181100), Gr_32 = c(203520000, 545960000, 17214000
                                                                                                                                ), Gr_33 = c(353320000, 1048300000, 31244000), 
               Gr_34 = c(163870000, 1026300000, 23291000), Gr_35 = c(134430000, 
                                                                                         773530000, 19527000), Gr_36 = c(159630000, 547270000, 
                                                                                                                                   12743000), Gr_37 = c(214110000, 311250000, 7546600
                                                                                                                                   ), Gr_38 = c(199410000, 118250000, 4917500), Gr_39 = c(73076000, 
                                                                                                                                                                                                              41591000, 2751100), Gr_4 = c(0, 348100000, 3.0977e+10
                                                                                                                                                                                                              ), Gr_40 = c(57703000, 28495000, 2336400), Gr_41 = c(91657000, 
                                                                                                                                                                                                                                                                                       11727000, 7568200), Gr_5 = c(0, 176770000, 8752700000
                                                                                                                                                                                                                                                                                       ), Gr_6 = c(0, 174870000, 6783500000), Gr_7 = c(0, 
                                                                                                                                                                                                                                                                                                                                                           138470000, 2386900000), Gr_8 = c(0, 84046000, 1371600000
                                                                                                                                                                                                                                                                                                                                                           ), Gr_9 = c(0, 82060000, 567990000)), .Names = c("H", 
                                                                                                                                                                                                                                                                                                                                                                                                                      "Gr_1", "Gr_10", "Gr_11", "Gr_12", 
                                                                                                                                                                                                                                                                                                                                                                                                                      "Gr_13", "Gr_14", "Gr_15", "Gr_16", 
                                                                                                                                                                                                                                                                                                                                                                                                                      "Gr_17", "Gr_18", "Gr_19", "Gr_2", 
                                                                                                                                                                                                                                                                                                                                                                                                                      "Gr_20", "Gr_21", "Gr_22", "Gr_23", 
                                                                                                                                                                                                                                                                                                                                                                                                                      "Gr_24", "Gr_25", "Gr_26", "Gr_27", 
                                                                                                                                                                                                                                                                                                                                                                                                                      "Gr_28", "Gr_29", "Gr_3", "Gr_30", 
                                                                                                                                                                                                                                                                                                                                                                                                                      "Gr_31", "Gr_32", "Gr_33", "Gr_34", 
                                                                                                                                                                                                                                                                                                                                                                                                                      "Gr_35", "Gr_36", "Gr_37", "Gr_38", 
                                                                                                                                                                                                                                                                                                                                                                                                                      "Gr_39", "Gr_4", "Gr_40", "Gr_41", 
                                                                                                                                                                                                                                                                                                                                                                                                                      "Gr_5", "Gr_6", "Gr_7", "Gr_8", 
                                                                                                                                                                                                                                                                                                                                                                                                                      "Gr_9"), row.names = c(NA, 3L), class = "data.frame")

我正在尝试应用函数sortorder,但它们都不会更改顺序。任何想法如何正确分类?我想从Gr_1开始:Gr_10然后Gr_11:" Gr_20"等

2 个答案:

答案 0 :(得分:3)

另一个版本可能更容易阅读:

n <- sub("Gr_", "", names(df))    # replace Gr_ with nothing
n[1] <- 0                         # keep "H" column as first
n <- as.numeric(n)                # convert to numeric (H becomes zero)
df[,order(n)]                     # use that to sort columns

(但我喜欢akrun建议使用gtools::mixedsort / -order)

答案 1 :(得分:1)

UITextAttributes att = new UITextAttributes();
att.Font = UIFont.SystemFontOfSize(20.0 , FontAttributes.Bold); // size and weight

UIBarButtonItem.AppearanceWhenContainedIn(new Type[] { typeof( UINavigationBar) }).SetTitleTextAttributes(att, UIControlState.Normal);
UIBarButtonItem.AppearanceWhenContainedIn(new Type[] { typeof(UINavigationBar) }).SetTitleTextAttributes(att,  UIControlState.Highlighted);

把H放在第一位:

d[,order(as.numeric(sub("\\D*","",colnames(d))))]
      Gr_1       Gr_2       Gr_3       Gr_4       Gr_5       Gr_6       Gr_7       Gr_8
1        0    1584100 4.2060e+06 0.0000e+00          0          0          0          0
2  1107200   84847000 2.9108e+08 3.4810e+08  176770000  174870000  138470000   84046000
3 17096000 5219500000 1.5496e+10 3.0977e+10 8752700000 6783500000 2386900000 1371600000
       Gr_9      Gr_10     Gr_11     Gr_12     Gr_13     Gr_14    Gr_15    Gr_16    Gr_17
1         0          0   1835800         0   2544200   5120400  6623500        0 13475000
2  82060000   37259000  53909000  19117000   2461400   1373700        0        0        0
3 567990000 1104800000 623960000 808600000 418770000 117330000 73336000 31761000 29387000
     Gr_18    Gr_19     Gr_20     Gr_21      Gr_22      Gr_23      Gr_24     Gr_25
1  7883300 82339000 205860000 867120000 1144300000 1179000000 1408700000 757020000
2        0  3254700         0   1984400   11342000    4391600    2830100   5588500
3 27476000 50825000  67685000 226000000  256440000  141230000  328190000 113360000
      Gr_26     Gr_27    Gr_28     Gr_29     Gr_30     Gr_31     Gr_32      Gr_33
1 456930000 162310000 71199000 220140000 418310000 214340000 203520000  353320000
2   4694700  12425000 13883000  48225000 248370000 342310000 545960000 1048300000
3  78221000  29132000 39425000  22240000   2998900   6181100  17214000   31244000
       Gr_34     Gr_35     Gr_36     Gr_37     Gr_38    Gr_39    Gr_40    Gr_41        H
1  163870000 134430000 159630000 214110000 199410000 73076000 57703000 91657000 P01050.1
2 1026300000 773530000 547270000 311250000 118250000 41591000 28495000 11727000 P01080.1
3   23291000  19527000  12743000   7546600   4917500  2751100  2336400  7568200 P01090.1