Ordering a character vector in R

I am trying to arrange a state vector. I understand that this should be very simple, but I cannot solve it. I looked at other posts that offer sophisticated solutions using vapply (...), but this seems unnecessary. I have the following:

state.vec = c("AL", "AK", "AZ", "AR", "CA", "CO", "CT", "DE", "CD", "FL", "AG", "HI", "DI", "IL", "IN", "AI", "KS", "KY", "AL", "EM", "DM", "AM", "IM", "MN", "MS", "MO", "MT", "EN", "NV", "HN", "JN", "MN", "NY", "CN", "DN", "HO", "KO", "OR", "AP", "PR", "IR", "CS", "DS", "NT", "TX", "TU", "TV", "IV", "AV", "AW","VW", "IW", "WY", "GU") 

Unfortunately, order converts the values ​​to their integer order:

  order(state.vec) 2 1 4 3 5 6 7 9 8 10 11 54 12 16 13 14 15 17 18 19 22 21 20 23 24 26 25 27 34 35 28 30 31 32 29 33 36 37 38 39 40 41 42 43 44 45 46 49 48 47 50 52 51 53 

It’s smart enough to admit that β€œAK” is preceded by β€œAL,” and therefore β€œAL” matches the second or integer 2. Ideally, I would like it to reorder the vector so that it goes from 1: 53 respectively, for example ("AK", "AL", "AR", ... etc.

+4
source share
1 answer
 state.vec[order(state.vec)] 

or simply:

 sort(state.vec) 
+16
source

All Articles