【问题标题】:Converting column names so they can be put in an numerical order转换列名,以便它们可以按数字顺序排列
【发布时间】:2022-07-08 15:10:53
【问题描述】:

我正在尝试扩展this answer,创建一个适用于new_datold_dat 的解决方案。

新数据

new_dat <- structure(list(`[0,25) east` = c(1269L, 85L), `[0,25) north` = c(364L, 
21L), `[0,25) south` = c(1172L, 97L), `[0,25) west` = c(549L, 
49L), `[100,250) east` = c(441L, 149L), `[100,250) north` = c(224L, 
45L), `[100,250) south` = c(521L, 247L), `[100,250) west` = c(770L, 
124L), `[100,500) east` = c(0L, 0L), `[100,500) north` = c(0L, 
0L), `[100,500) south` = c(0L, 0L), `[100,500) west` = c(0L, 
0L), `[1000,1000000] east` = c(53L, 0L), `[1000,1000000] north` = c(82L, 
0L), `[1000,1000000] south` = c(23L, 0L), `[1000,1000000] west` = c(63L, 
0L), `[1000,1500) east` = c(0L, 0L), `[1000,1500) north` = c(0L, 
0L), `[1000,1500) south` = c(0L, 0L), `[1000,1500) west` = c(0L, 
0L), `[1500,3000) east` = c(0L, 0L), `[1500,3000) north` = c(0L, 
0L), `[1500,3000) south` = c(0L, 0L), `[1500,3000) west` = c(0L, 
0L), `[25,100) east` = c(579L, 220L), `[25,100) north` = c(406L, 
58L), `[25,100) south` = c(1048L, 316L), `[25,100) west` = c(764L, 
131L), `[25,50) east` = c(0L, 0L), `[25,50) north` = c(0L, 0L
), `[25,50) south` = c(0L, 0L), `[25,50) west` = c(0L, 0L), `[250,500) east` = c(232L, 
172L), `[250,500) north` = c(207L, 40L), `[250,500) south` = c(202L, 
148L), `[250,500) west` = c(457L, 153L), `[3000,1000000] east` = c(0L, 
0L), `[3000,1000000] north` = c(0L, 0L), `[3000,1000000] south` = c(0L, 
0L), `[3000,1000000] west` = c(0L, 0L), `[50,100) east` = c(0L, 
0L), `[50,100) north` = c(0L, 0L), `[50,100) south` = c(0L, 0L
), `[50,100) west` = c(0L, 0L), `[500,1000) east` = c(103L, 0L
), `[500,1000) north` = c(185L, 0L), `[500,1000) south` = c(66L, 
0L), `[500,1000) west` = c(200L, 0L), `[500,1000000] east` = c(0L, 
288L), `[500,1000000] north` = c(0L, 120L), `[500,1000000] south` = c(0L, 
229L), `[500,1000000] west` = c(0L, 175L)), row.names = c("A", 
"B"), class = "data.frame")

旧数据和原始解决方案

old_dat <- structure(list(`[0,25)` = 5L, `[100,250)` = 43L, `[100,500)` = 0L, 
    `[1000,1000000]` = 20L, `[1000,1500)` = 0L, `[1500,3000)` = 0L, 
    `[25,100)` = 38L, `[25,50)` = 0L, `[250,500)` = 27L, `[3000,1000000]` = 0L, 
    `[50,100)` = 0L, `[500,1000)` = 44L, `[500,1000000]` = 0L), row.names = "Type_A", class = "data.frame")

该解决方案利用了这样一个事实,即添加的每个列名称中的两个数字之和提供了正确的顺序。

ord <- gsub("\\[|\\]|\\)", "", colnames(new_dat)) %>% 
         strsplit(",") %>% 
         lapply(as.numeric) %>% 
         lapply(sum) %>% 
         unlist %>% 
         order()

colnames(dat)[ord]

新方法

新数据不仅可以是数值,还可以是字符串值(east, north, south, west)。我意识到如果我给east 赋值1north2 等等,我可以使用相同的解决方案。这三个数字的总和仍然提供正确的顺序。

不过,我在调整代码时遇到了一些麻烦。

ord <- gsub("\\[|\\]|\\)", "", colnames(new_dat)) %>% 
         # provides "0,25 east", "0,25 north" etc

         strsplit(",") %>% 
         # provides "0" and "25 east", "0" and "25 north" etc

         lapply(as.numeric) %>% 
         lapply(sum) %>% 
         # SHOULD provide 0+25+1 (east), 0+25+2 (north) etc

         unlist %>% 
         order()

问题在于将字符串拆分为 3 部分,并将方向转换为数字,IF 和 ONLY IF,有 3 部分。否则它应该只使用这两个。我该怎么做?

【问题讨论】:

  • 名称中只有一个空格,所以s1 &lt;- strsplit(names(new_dat), " "); lengths(s1) 将为您提供包含 3 个部分的字符串。这有帮助吗?

标签: r dplyr strsplit


【解决方案1】:

要构建您的解决方案,您可以这样做,

ord <- gsub("\\D+", ",", stri_replace_all_regex(names(new_dat), '[A-Za-z]', 1:4)) %>% 
     strsplit(",") %>% 
     lapply(as.numeric) %>% 
     lapply(sum, na.rm = TRUE) %>% 
     unlist() %>% 
     order()

> names(new_dat)[ord]
 [1] "[0,25) east"          "[0,25) south"         "[0,25) north"         "[0,25) west"          "[25,50) east"         "[25,50) south"        "[25,50) north"        "[25,50) west"         "[25,100) east"        "[25,100) south"      
[11] "[25,100) north"       "[25,100) west"        "[50,100) east"        "[50,100) south"       "[50,100) north"       "[50,100) west"        "[100,250) east"       "[100,250) south"      "[100,250) north"      "[100,250) west"      
[21] "[100,500) east"       "[100,500) south"      "[100,500) north"      "[100,500) west"       "[250,500) east"       "[250,500) south"      "[250,500) north"      "[250,500) west"       "[500,1000) east"      "[500,1000) south"    
[31] "[500,1000) north"     "[500,1000) west"      "[1000,1500) east"     "[1000,1500) south"    "[1000,1500) north"    "[1000,1500) west"     "[1500,3000) east"     "[1500,3000) south"    "[1500,3000) north"    "[1500,3000) west"    
[41] "[500,1000000] east"   "[500,1000000] south"  "[500,1000000] north"  "[500,1000000] west"   "[1000,1000000] east"  "[1000,1000000] south" "[1000,1000000] north" "[1000,1000000] west"  "[3000,1000000] east"  "[3000,1000000] south"
[51] "[3000,1000000] north" "[3000,1000000] west" 

【讨论】:

  • 非常感谢!如果我有其他名字,是否有可能用"\\d+之类的东西替换模式pattern = c("east", "south", "north", "west")
  • 但是它们将如何映射呢?哪个将是 1,哪个 2,等等..?或者那没关系
  • 不,顺序不是那么重要。在完美的情况下,代码只会计算类别的数量(东、北、南、西)并为它们提供一个数字。例如,如果它也可以处理 5 个类别,那就太好了。如果我改变一些东西,它就不太可能崩溃。
  • 好的,现在应该可以正常工作了
  • 非常感谢 Sotos。我会接受 JBGruber 的答案,因为它扩展到超过 4 个类别,并且它也适用于 old_dat
【解决方案2】:

也许有点矫枉过正,但有了这个,你不需要找到“东”、“南”等模式。

library(magrittr)
order_cols <- function(dat) {
  
  # look for words to order by
  s_ordered <- stringi::stri_extract_all_regex(colnames(dat), "[[:alpha:]]+") %>% 
    unlist() %>% 
    unique() %>% 
    sort()
  
  if (length(s_ordered) > 1) {
    # replace words with their alphabetical index
    cnames <- stringi::stri_replace_all_fixed(colnames(dat), s_ordered, seq_along(s_ordered), vectorise_all = FALSE)
  } else {
    cnames <- colnames(dat)
  }
  
  cnames %>% 
    stringi::stri_extract_all_regex("\\d+") %>% # extract all numbers (including the alphabetical index numbers)
    lapply(as.numeric) %>% 
    lapply(sum) %>% 
    unlist() %>% 
    order()
  
}

在函数的第一部分,我从列名中提取字符串并对其进行排序。然后使用它们的顺序将 colnames 中的单词替换为它们的索引。之后,我提取数值并几乎遵循您的初始方法。我把它放在一个函数中使它更容易使用:

colnames(new_dat)[order_cols(new_dat)]
#>  [1] "[0,25) east"          "[0,25) north"         "[0,25) south"        
#>  [4] "[0,25) west"          "[25,50) east"         "[25,50) north"       
#>  [7] "[25,50) south"        "[25,50) west"         "[25,100) east"       
#> [10] "[25,100) north"       "[25,100) south"       "[25,100) west"       
#> [13] "[50,100) east"        "[50,100) north"       "[50,100) south"      
#> [16] "[50,100) west"        "[100,250) east"       "[100,250) north"     
#> [19] "[100,250) south"      "[100,250) west"       "[100,500) east"      
#> [22] "[100,500) north"      "[100,500) south"      "[100,500) west"      
#> [25] "[250,500) east"       "[250,500) north"      "[250,500) south"     
#> [28] "[250,500) west"       "[500,1000) east"      "[500,1000) north"    
#> [31] "[500,1000) south"     "[500,1000) west"      "[1000,1500) east"    
#> [34] "[1000,1500) north"    "[1000,1500) south"    "[1000,1500) west"    
#> [37] "[1500,3000) east"     "[1500,3000) north"    "[1500,3000) south"   
#> [40] "[1500,3000) west"     "[500,1000000] east"   "[500,1000000] north" 
#> [43] "[500,1000000] south"  "[500,1000000] west"   "[1000,1000000] east" 
#> [46] "[1000,1000000] north" "[1000,1000000] south" "[1000,1000000] west" 
#> [49] "[3000,1000000] east"  "[3000,1000000] north" "[3000,1000000] south"
#> [52] "[3000,1000000] west"


colnames(dat)[order_cols(dat)]
#>  [1] "[0,25)"         "[25,50)"        "[25,100)"       "[50,100)"      
#>  [5] "[100,250)"      "[100,500)"      "[250,500)"      "[500,1000)"    
#>  [9] "[1000,1500)"    "[1500,3000)"    "[500,1000000]"  "[1000,1000000]"
#> [13] "[3000,1000000]"

reprex package (v2.0.1) 于 2022-05-06 创建

P.S.:如果您使用的是较新版本的 R (>= 4.10),您可以使用原生管道 (|&gt;) 代替 magrittr%&gt;%

【讨论】:

  • 这太棒了!我添加了第五个类别(通过将north 更改为A),它仍然可以解决问题。这正是我需要让它对变化更加健壮!
  • 一件非常小的事情,这并不重要,因为我可以想到一个解决方法(通过使用正则表达式来查看是否有字符串),但是否也可以让它工作旧数据?
  • 抱歉,我以为我做到了,但我注意到我运行了两次new_dat
  • 现在可以使用了。使代码更复杂但并不多。
猜你喜欢
  • 2020-01-16
  • 2019-04-09
  • 1970-01-01
  • 2020-03-29
  • 2014-07-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多