在R中,如何对其中带有“ _”的字符串列名称进行排序

时间:2019-09-19 17:22:17

标签: r sorting dataframe

我有这个数据,

[collection_id] => 2

我希望此顺序为

Array
(
    [0] => Array
        (
            [id] => 2
            [name] => Name of Collection
            [operator_id] => 1
            [location_id] => 4
            [account_id] => 1
            [date_time] => 2019-09-16 04:41:26
            [reconciliation_id] => 
            [created_by_id] => 1
            [updated_by_id] => 1
            [created_at] => 2019-09-16 23:44:43
            [updated_at] => 2019-09-18 17:32:53
            [equipment] => Array
                (
                    [0] => Array
                        (
                            [id] => 1
                            [name] => Name of Equipment
                            [ims_identifier] => AFA-64
                            [operator_identifier] => 073076-01
                            [equipment_model_id] => 1
                            [operator_id] => 1
                            [location_id] => 4
                            [created_by_id] => 1
                            [updated_by_id] => 1
                            [created_at] => 2019-07-17 13:17:28
                            [updated_at] => 2019-08-14 00:04:07
                            [pivot] => Array
                                (
                                    [collection_id] => 2
                                    [equipment_id] => 1
                                    [created_at] => 2019-09-17 00:17:00
                                    [updated_at] => 2019-09-17 00:17:00
                                )

                            [collection_meters] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 9
                                            [equipment_meter_id] => 1
                                            [value] => 0.25
                                            [gross] => 24.00
                                            [refund] => 2.00
                                            [test] => 1.00
                                            [reading_start] => 72885
                                            [reading_end] => 72985
                                            [collection_id] => 2
                                            [equipment_id] => 1
                                            [operator_id] => 1
                                            [location_id] => 4
                                            [account_id] => 1
                                            [created_by_id] => 1
                                            [updated_by_id] => 1
                                            [created_at] => 2019-09-17 00:17:00
                                            [updated_at] => 2019-09-18 17:32:53
                                        )

                                )

                        )

我尝试了几种不同的方法,但是它们并没有得到如上所述的结果。

请帮助。

2 个答案:

答案 0 :(得分:2)

我们可以使用mixedsort中的mixedordergtools

gtools::mixedsort(names(df))
#[1] "V1"    "V1_1"  "V1_2"  "V1_10" "V2"   

df[gtools::mixedsort(names(df))]

答案 1 :(得分:1)

gtools::mixedsort似乎是最精巧的解决方案,但是下面我添加了一些代码,无需使用专门的库即可将您带到同一个地方...这些代码可以在其他情况下重新使用...

定义以下函数,并与sapply组合以将该函数应用于列名。这将为您提供一个数值矩阵,以便进行排序:第一行对应于列名称中“ V”旁边的值,而第二行对应于列名称中“ _”之后的数字。

> convert_Name_To_Numbers <- function(x){
+   tempVec <- strsplit(x, "_")[[1]]
+   resultVec <- c(gsub("V","", tempVec[1]), 
+                  ifelse(is.na(tempVec[2]),"0", tempVec[2]))
+   return(as.double(resultVec))
+ }

> sortVec <- sapply(names(df), convert_Name_To_Numbers)
> sortVec
     V1 V1_1 V1_10 V1_2 V2
[1,]  1    1     1    1  2
[2,]  0    1    10    2  0

接下来使用order使用这两行对列名进行重新排序,即首先在第一行上排序,然后使用第二行值。

> newColumns <- names(df)[order(sortVec[1,], sortVec[2,])]
> newColumns
[1] "V1"    "V1_1"  "V1_2"  "V1_10" "V2"

希望很明显,可以轻松地对代码进行调整,以处理更复杂的列名,而您需要两个以上的排序标准,例如V1,V1_1,V1_1_1,V_1_2,...