1

I have a vector of strings in R that follow this format: 1:A. So let's say I have a vector with strings

x = c("1:A", "201:A", "2:A") 

I want to sort through this vector so it becomes

["1:A" "2:A" "201:A"]

Is there a function that is capable of this in R? I have tried

mixedsort(x, decreasing = FALSE) 

from the gtools library, but it still doesn't seem to completely work very well when this vector is scaled up to include the letter B as well. Any ideas?

mrk
  • 8,059
  • 3
  • 56
  • 78
zorian15
  • 55
  • 9
  • what are your example data and expected result when the "vector is scaled up to include the letter B as well". – dww Jun 27 '17 at 18:10

1 Answers1

3

One option is mixedsort

library(gtools)
mixedsort(x)
#[1] "1:A"   "2:A"   "201:A"

Or remove the non-numeric characters with gsub and order

x[order(as.numeric(gsub("\\D+", "", x)))]
#[1] "1:A"   "2:A"   "201:A"
akrun
  • 874,273
  • 37
  • 540
  • 662