Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Case insensitive sort of vector of string in R

Tags:

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"
like image 528
pdubois Avatar asked Apr 27 '15 08:04

pdubois


3 Answers

Try

mylist[order(tolower(mylist))]
like image 150
lukeA Avatar answered Sep 25 '22 21:09

lukeA


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"
like image 45
David Arenburg Avatar answered Sep 24 '22 21:09

David Arenburg


> 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" 
like image 32
Prasanna Nandakumar Avatar answered Sep 22 '22 21:09

Prasanna Nandakumar