1

大家好,我正在尝试解决 R 中的问题。我想读取分配在主文件夹中的多个子文件夹中的大量文件。然后我曾经list.files()获取子文件夹,但是当我使用该功能时,由于b矢量的顺序不同,它会出错。向量b是下一个:

b=list.files()
b
 [1] "A1"  "A10" "A11" "A12" "A13" "A14" "A15" "A2"  "A3"  "A4"  "A5"  "A6"  "A7" 
[14] "A8"  "A9" 

其结构如下:

chr [1:5] "A1" "A2" "A3" "A4" "A5"

我希望我的b向量采用这种形式:

b
 [1] "A1" "A2"  "A3"  "A4"  "A5"  "A6"  "A7" "A8"  "A9" "A10" "A11" "A12" "A13"
[14] "A14" "A15"

谢谢你的帮助!

4

4 回答 4

2

有几种方法可以做到这一点。这是另一个:

b <- c("A1","A10","A11","A12","A13","A14","A15","A2","A3","A4","A5","A6","A7")
b[order(nchar(b), b)]
# [1] "A1"  "A2"  "A3"  "A4"  "A5"  "A6"  "A7"  "A10" "A11" "A12" "A13" "A14" "A15"
于 2013-10-21T20:03:44.847 回答
2

我建议不要专注于特定示例,而是使用现有工具,mixedsort在 gtools 包中处理不规则宽度的字母和数字字符。

require(gtools)
vec <- paste0( replicate(40, {
            paste( sample(LETTERS, 3, repl=TRUE),collapse="")}),  
                sample(1:400, 40, repl=TRUE) ) 
mixedsort(vec)
 [1] "ABP256" "ATV361" "ATZ12"  "BKL273" "BOY273" "BQJ242" "CQL129"
 [8] "CXH313" "CXQ249" "DFU116" "FGI305" "HJK249" "ICN4"   "IML75" 
[15] "JDJ309" "JEB93"  "JHF276" "JIY265" "JXK287" "KCQ282" "MAR161"
[22] "MGV185" "MHH72"  "NDJ84"  "NGZ84"  "OIV207" "ORK31"  "PSJ95" 
[29] "QOC178" "QXL344" "QYK285" "RFO98"  "ROC135" "TUL40"  "UBT134"
[36] "UKP14"  "VQL372" "YLG393" "ZLD394" "ZSG180"

如果mixedsort不存在,人类将需要发明它。这并不完全会产生相同的结果,但它可能会照亮前进的道路:

vec[ order( gsub("[[:digit:]]", "", vec), gsub("[[:alpha:]]", "", vec) )]
 [1] "ABP256" "ATV361" "ATZ12"  "BKL273" "BOY273" "BQJ242" "CQL129"
 [8] "CXH313" "CXQ249" "DFU116" "FGI305" "HJK249" "ICN4"   "IML75" 
[15] "JDJ309" "JEB93"  "JHF276" "JIY265" "JXK287" "KCQ282" "MAR161"
[22] "MGV185" "MHH72"  "NDJ84"  "NGZ84"  "OIV207" "ORK31"  "PSJ95" 
[29] "QOC178" "QXL344" "QYK285" "RFO98"  "ROC135" "TUL40"  "UBT134"
[36] "UKP14"  "VQL372" "YLG393" "ZLD394" "ZSG180"

它将所有字母字符折叠在数字之前,并将order“a12b”放在“a99z”之前。

于 2013-10-21T20:14:57.360 回答
2

gtools::mixedsort专为此设计...

gtools::mixedsort( c("A1","A10","A11","A12","A13","A14","A15","A2","A3","A4","A5","A6","A7") )
[1] "A1"  "A2"  "A3"  "A4"  "A5"  "A6"  "A7"  "A10" "A11" "A12" "A13" "A14" "A15"

感谢@Matthew 提供数据

于 2013-10-21T20:09:36.443 回答
1

如果这是您拥有的确切数据,您可以按照您想要的方式对其进行排序,如下所示:

> b[order(as.integer(substr(b,2,3)))]
 [1] "A1"  "A2"  "A3"  "A4"  "A5"  "A6"  "A7"  "A8"  "A9"  "A10" "A11" "A12"
[13] "A13" "A14" "A15"

如果实际数据更复杂,您可能需要做更多的字符串操作,但这是一般的想法。

于 2013-10-21T19:48:29.780 回答