大家好我想解决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"
感谢您的帮助!
答案 0 :(得分: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"
答案 1 :(得分: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的数据
答案 2 :(得分:2)
我建议使用gtools包中的现有工具mixedsort
而不是专注于某个特定示例,该工具处理字母和数字字符的不规则宽度。
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"
它会在数字之前将所有字母字符折叠在一起,并在“a99z”之前order
“a12b”。
答案 3 :(得分: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"
如果实际数据更复杂,你可能需要做更多的字符串操作,但这是一般的想法。