12

I was wondering if there is an already made function in R base package that can sort a vector of strings taking into consideration the length of each element and then of course the lexicographical order. For instance after a sort call on some vector holding age groups you would have:

v <- c("00-04", "05-09", "10-14", "100-104", "105-109", "110-114", "15-19", "20-24"..etc)

whereas I would like to have:

v <- c("00-04", "05-09", "10-14", "15-19", "20-24"..etc.. "100-104", "105-109", "110-114")
ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
Marius
  • 990
  • 1
  • 14
  • 34

2 Answers2

29

Simply with order :

v[order(nchar(v), v)]

## [1] "00-04"   "05-09"   "10-14"   "15-19"   "20-24"   "100-104" "105-109" "110-114"

Is that what you're looking for?

Victorp
  • 13,636
  • 2
  • 51
  • 55
  • Yeah. I will accept this as the "official" answer even though Ananda was the first to provide the solution. Thank you. – Marius Mar 11 '14 at 12:39
  • @Marius, no problem. I was on my phone so I couldn't verify my answer, and I don't like posting answers that I don't know work. :-) – A5C1D2H2I1M1N2O1R2T1 Mar 11 '14 at 13:12
3

Not in R base, but this splits the strings in numeric and character parts and sorts appropriately:

v <- c("00-04", "05-09", "10-14", "100-104", "105-109", "110-114", "15-19", "20-24")
library(gtools)
mixedsort(v)
#[1] "00-04"   "05-09"   "10-14"   "15-19"   "20-24"   "100-104" "105-109" "110-114"

You can always copy the code of the mixedorder function defined in the gtools package if you don't want to load/depend on it.

Roland
  • 127,288
  • 10
  • 191
  • 288
  • I chose the answer Ananda gave as it does not need another library. Thank you for your answer also. – Marius Mar 11 '14 at 12:35