简体   繁体   中英

Case insensitive sort of vector of string in R

I have the following vector:

 mylist <- c("MBT.LN.ID", "ISA51VG.LN.ID", "R848.LN.ID", "sHz.LN.ID", "FK565.LN.ID",
    "bCD.LN.ID", "MALP2s.LN.ID", "ADX.LN.ID", "AddaVax.LN.ID", "FCA.LN.ID",
    "Pam3CSK4.LN.ID", "D35.LN.ID", "ALM.LN.ID", "K3.LN.ID", "K3SPG.LN.ID",
    "MPLA.LN.ID", "DMXAA.LN.ID", "cGAMP.LN.ID", "Poly_IC.LN.ID",
    "cdiGMP.LN.ID")

I'd like to sort them alphabetically in case-insensitive manner.

The expected output is this:

 [1] "AddaVax.LN.ID"  "ADX.LN.ID"      "ALM.LN.ID"      "bCD.LN.ID"      "cdiGMP.LN.ID"   "cGAMP.LN.ID"   
 [7] "D35.LN.ID"      "DMXAA.LN.ID"    "FCA.LN.ID"      "FK565.LN.ID"    "ISA51VG.LN.ID"  "K3.LN.ID"      
[13] "K3SPG.LN.ID"    "MALP2s.LN.ID"   "MBT.LN.ID"      "MPLA.LN.ID"     "Pam3CSK4.LN.ID" "Poly_IC.LN.ID" 
[19] "R848.LN.ID"     "sHz.LN.ID"   

I tried this but failed (Using R.3.2.0 alpha):

> sort(mylist)
 [1] "ADX.LN.ID"      "ALM.LN.ID"      "AddaVax.LN.ID"  "D35.LN.ID"
 [5] "DMXAA.LN.ID"    "FCA.LN.ID"      "FK565.LN.ID"    "ISA51VG.LN.ID"
 [9] "K3.LN.ID"       "K3SPG.LN.ID"    "MALP2s.LN.ID"   "MBT.LN.ID"
[13] "MPLA.LN.ID"     "Pam3CSK4.LN.ID" "Poly_IC.LN.ID"  "R848.LN.ID"
[17] "bCD.LN.ID"      "cGAMP.LN.ID"    "cdiGMP.LN.ID"   "sHz.LN.ID"

尝试

mylist[order(tolower(mylist))]

As noted by @Pascal, this is documented in help(Comparison) and sort is local specific. One Option is switching your local (for example Sys.setlocale("LC_TIME", "us") ), but that could be inconvenient. Another option could be using gtools::mixedsort which could be also useful because you string also contains numbers.

library(gtools)
mixedsort(mylist)

# [1] "AddaVax.LN.ID"  "ADX.LN.ID"      "ALM.LN.ID"      "bCD.LN.ID"      "cdiGMP.LN.ID"   "cGAMP.LN.ID"    "D35.LN.ID"      "DMXAA.LN.ID"    "FCA.LN.ID"      "FK565.LN.ID"   
# [11] "ISA51VG.LN.ID"  "K3.LN.ID"       "K3SPG.LN.ID"    "MALP2s.LN.ID"   "MBT.LN.ID"      "MPLA.LN.ID"     "Pam3CSK4.LN.ID" "Poly_IC.LN.ID"  "R848.LN.ID"     "sHz.LN.ID"
> library(searchable)
> sort(ignore.case(mylist))
 [1] "AddaVax.LN.ID"  "ADX.LN.ID"      "ALM.LN.ID"      "bCD.LN.ID"      "cdiGMP.LN.ID"  
 [6] "cGAMP.LN.ID"    "D35.LN.ID"      "DMXAA.LN.ID"    "FCA.LN.ID"      "FK565.LN.ID"   
[11] "ISA51VG.LN.ID"  "K3.LN.ID"       "K3SPG.LN.ID"    "MALP2s.LN.ID"   "MBT.LN.ID"     
[16] "MPLA.LN.ID"     "Pam3CSK4.LN.ID" "Poly_IC.LN.ID"  "R848.LN.ID"     "sHz.LN.ID" 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM