How to order a character vector in ascend form in R

前端 未结 4 1973
感动是毒
感动是毒 2021-01-16 11:35

Hi everybody I am trying to solve a problem in R. I want to read a lot of files allocated in multiples sub folders in a main folder. Then I used list.files() t

4条回答
  •  梦谈多话
    2021-01-16 12:07

    Rather than focussing on a particular example I suggest using an existing tool, mixedsort in the gtools package that handles irregular widths of both the alpha and numeric characters.

    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"
    

    If mixedsort didn't exist man would need to invent it. This is not exactly going to produce the same results but it might light a path forward:

    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"
    

    It collapse all the alpha characters together before the numerics and would order "a12b" before "a99z".

提交回复
热议问题