Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to perform natural (lexicographic) sorting in R? [duplicate]

Is there a natural sort for R?

Say I had a character vector like so:

seq.names <- c('abc21', 'abc2', 'abc1', 'abc01', 'abc4', 'abc201', '1b', '1a')

I'd like to sort it aphanumerically, so I get back this:

c('1a', '1b', 'abc1', 'abc01', 'abc2', 'abc4', 'abc21', 'abc201')

Does this exist somewhere, or should I start coding?

like image 356
cbare Avatar asked May 06 '10 02:05

cbare


2 Answers

I don't think "alphanumeric sort" means what you think it means.

In any case, looks like you want mixedsort, part of gtools.

> install.packages('gtools')
[...]
> require('gtools')
Loading required package: gtools
> n
[1] "abc21"  "abc2"   "abc1"   "abc01"  "abc4"   "abc201" "1b"     "1a"    
> mixedsort(n)
[1] "1a"     "1b"     "abc1"   "abc01"  "abc2"   "abc4"   "abc21"  "abc201"
like image 100
Nicholas Riley Avatar answered Nov 03 '22 22:11

Nicholas Riley


Natural sorting is available in the stringr/stringi packages with the functions str_sort()/stri_sort(). Switching between alphanumeric and natural sorting is controlled by the 'numeric' argument.

library(stringr)
# library(stringi)

str_sort(seq.names, numeric = TRUE)
# stri_sort(seq.names, numeric = TRUE)

[1] "1a"     "1b"     "abc1"   "abc01"  "abc2"   "abc4"   "abc21"  "abc201"

The companion function str_order() / stri_order() returns the indices to arrange the vector in (by default) ascending order:

str_order(seq.names, numeric = TRUE)
# stri_order(seq.names, numeric = TRUE)

[1] 8 7 3 4 2 5 1 6

seq.names[str_order(seq.names, numeric = TRUE)]

[1] "1a"     "1b"     "abc1"   "abc01"  "abc2"   "abc4"   "abc21"  "abc201"
like image 25
Ritchie Sacramento Avatar answered Nov 03 '22 21:11

Ritchie Sacramento