0

I'm trying to sort these state names by alphabetical order while retaining the number to the left of the state name. I currently cannot figure out how to do this.

I've tried using various forms of gsub in an attempt to remove the numbers before sorting without any success.

This is the dataset with the states:

print(StateRankings)

# [1] "1. Arizona"         "10. Missouri"       "11. Tennessee"      "12. Florida"       
# [5] "13. West Virginia"  "14. Kentucky"       "15. New Hampshire"  "16. Mississippi"   
# [9] "17. Wyoming"        "18. Alabama"        "19. Idaho"           "2. Alaska"         
#[13] "20. Vermont"        "21. Indiana"        "22. Arkansas"       "23. Wisconsin"     
#[17] "24. South Carolina" "25. Nevada"         "26. North Carolina" "27. Michigan"      
#[21] "28. Louisiana"      "29. Ohio"           "3. Kansas"          "30. Maine"         
#[25] "31. Virginia"       "32. South Dakota"   "33. Pennsylvania"   "34. Oregon"        
#[29] "35. Nebraska"       "36. Iowa"           "37. New Mexico"     "38. Washington"    
#[33] "39. Colorado"       "4. Oklahoma"        "40. Illinois"       "41. Minnesota"     
#[37] "42. Delaware"       "43. Rhode Island"   "44. Maryland"       "45. Connecticut"   
#[41] "46. California"     "47. Hawaii"         "48. New Jersey"     "49. Massachusetts" 
#[45] "5. Montana"         "50. New York"       "6. Utah"             "7. North Dakota"   
#[49] "8. Texas"           "9. Georgia"
Ronak Shah
  • 286,338
  • 16
  • 97
  • 143
TimApple23
  • 13
  • 1

1 Answers1

2

We can remove the numbers and dot from the character vector, then use order to sort only the names and subset the original vector.

StateRankings[order(sub("^\\d+\\.\\s+", "", StateRankings))]

#[1] "18. Alabama"  "2. Alaska"  "1. Arizona"  "12. Florida"  "19. Idaho"        
# 6] "14. Kentucky"  "16. Mississippi"  "10. Missouri" "15. New Hampshire"     
#[10] "11. Tennessee" "13. West Virginia" "17. Wyoming" 

Just FYI, R has inbuilt state names which is in ascending order stored in state.name

state.name
#[1] "Alabama"   "Alaska"  "Arizona"  "Arkansas"  "California" "Colorado"
#[7] "Connecticut"  "Delaware"  "Florida"   "Georgia"  "Hawaii" "Idaho"........

data

StateRankings <- c("1. Arizona", "10. Missouri", "11. Tennessee" ,"12. Florida",
 "13. West Virginia" ,"14. Kentucky", "15. New Hampshire", "16. Mississippi",
 "17. Wyoming", "18. Alabama", "19. Idaho" ,"2. Alaska")
Ronak Shah
  • 286,338
  • 16
  • 97
  • 143