1

I have a little problem with sorting in R in presence of character vectors. I would like to sort

x<- c("q1","q4","q40","q10")

w.r.t. the numbers 1<4<10<40 appearing in its components. I n other words, I would like to arrive at the following output

[1] "q1"  "q4" "q10"  "q40"

Applying sort to x I arrive at

 sort(x)
 [1] "q1"  "q10" "q4"  "q40"

instead (this output makes sense to me, but it is not what I would like to obtain). I thought about removing the characters "" and q in each component, and then apply sort to the resulting numeric vector. I would like to try something simpler, though. Any help is much appreciated!

I thank you all.

Avitus
  • 734
  • 2
  • 14
  • 27
  • 2
    Have a look here: http://stackoverflow.com/questions/15551914/is-it-possible-to-sort-a-vector-of-alphanumeric-values-using-lexical-ordering-in/15556495#15556495. Don't know what you consider simple, but check `mixedsort` in `gtools` package. – Henrik Sep 24 '13 at 19:08
  • 2
    http://stackoverflow.com/questions/2778039/how-to-do-an-alphanumeric-sort-in-r/2778060#2778060 – Henrik Sep 24 '13 at 19:17
  • mixedsort works very well: I thank you, Henrik! – Avitus Sep 24 '13 at 19:18

0 Answers0