按 data.table R 中的模式对某些列重新排序

Reorder certain columns by a pattern within a data.table R

我有一个 data.table 以下列名称

> names(test)
 [1] "Week"                "PSRS_Argentina"      "PSRS_Australia"      "PSRS_Belgium"        "PSRS_Canada"         "PSRS_France"        
 [7] "PSRS_Germany"        "PSRS_Hungary"        "PSRS_Israel"         "PSRS_Italy"          "PSRS_Japan"          "PSRS_Korea, South"  
[13] "PSRS_Peru"           "PSRS_Poland"         "PSRS_Russia"         "PSRS_Spain"          "PSRS_Taiwan"         "PSRS_United Kingdom"
[19] "PSRS_United States"  "AR_Argentina"        "AR_Australia"        "AR_Belgium"          "AR_Canada"           "AR_France"          
[25] "AR_Germany"          "AR_Hungary"          "AR_Israel"           "AR_Italy"            "AR_Japan"            "AR_Korea, South"    
[31] "AR_Peru"             "AR_Poland"           "AR_Russia"           "AR_Spain"            "AR_Taiwan"           "AR_United Kingdom"  
[37] "AR_United States"    "totalPSRS"           "totalAR"  

我正在尝试对只有国家/地区名称的列名称重新排序。我意识到我可能必须在多个步骤中完成此操作,但我怎样才能将 PSRS_AR_ 分组,这样我会得到这样的结果:

[1] "Week"   "PSRS_Argentina"   "AR_Argentina"   "PSRS_Australia"   "AR_Australia" ... "totalPSRS" "totalAR"

我看到了 greplgrep 的一些用法,它们在这里可能会有帮助,但对 data.table 没有帮助。我试过这样做:

test[order(test)][order(-(grepl('*_[A-Z]',test[order(test)]))+1L]

但是没有对列进行排序。

这应该有效。将来,请提供一个 minimal, reproducible example,因为它使问题更容易回答(因此更有可能吸引答案!)

## I use the stringr package
### I find it easier to understand
require(stringr)
#> Loading required package: stringr

test <- data.frame(
  Week = c("week1", "week2", "week3"), 
  PSRS_Germany = c("some", "data", "idk"),
  PSRS_Israel = c("this", "is", "data"),
  AR_Germany = c("yes", "it's", "data"),
  AR_Israel = c("hmm", "look", "values"),
  totalPSRS = c("yes", "yeah", "hmm"),
  totalAR = c("foo", "bar", "BIFF!")
)

test
#>    Week PSRS_Germany PSRS_Israel AR_Germany AR_Israel totalPSRS totalAR
#> 1 week1         some        this        yes       hmm       yes     foo
#> 2 week2         data          is       it's      look      yeah     bar
#> 3 week3          idk        data       data    values       hmm   BIFF!

## Get just the names we want to rearrange
testNames <- names(test)
testNames <- testNames[str_detect(testNames, "_")]

## Rearragne those names
orderedNames <- 
  testNames[
    order(str_extract(testNames, "_.+(?=$)"),   # order by what's between string start and '_'
          str_extract(testNames, "(?<=^).+_"))  # _then_ by what's between '_' and string end
  ]

## Index using newly rearranged names
test[,c("Week", 
       orderedNames,
       "totalPSRS", "totalAR")]
#>    Week AR_Germany PSRS_Germany AR_Israel PSRS_Israel totalPSRS totalAR
#> 1 week1        yes         some       hmm        this       yes     foo
#> 2 week2       it's         data      look          is      yeah     bar
#> 3 week3       data          idk    values        data       hmm   BIFF!

reprex package (v2.0.1)

于 2021-09-13 创建