[R] Custom Sort Character and Numeric

swonder03 ramey.steven at gmail.com
Sun Oct 16 10:47:12 CEST 2011


I"m trying to do a custom sort in this order: 

1) Numeric digit furthest right; 
2) Alphabetical second furthest to the right;
3) Alphabetical the rest of the string beginning with the first character;

The example code I'm using is an array that follows:

/myArray <- c('AFP9','AFR9','TLQP7','AFS9','AFR8','AFP8','AFS7','TLQS8')/

The output I desire is:

/>myArray
[1] "AFS7" "AFP8" "AFR8"  "AFP9" "AFR9" "AFS9" "TLQP7" TLQS8"  /

What I'm thinking is writing a function that will order it by analyzing it
from right to left. Ideally there would be a way to look at the individual
strings like the formula in Excel "=RIGHT(cell, 1)" and drop the furthest
right then do the same thing to the next character. I've been looking into
custom sort for R and haven't found much. Any idea what this function would
look like? Possibly a while loop? Each string would have a length of at
least 3, possibly longer. Thank you in advance. 



--
View this message in context: http://r.789695.n4.nabble.com/Custom-Sort-Character-and-Numeric-tp3909058p3909058.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list