如何通过删除名称开头的数字按字母顺序对数据进行排序[重复]
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何通过删除名称开头的数字按字母顺序对数据进行排序[重复]相关的知识,希望对你有一定的参考价值。
这个问题在这里已有答案:
我正在尝试按字母顺序对这些状态名称进行排序,同时保留状态名称左侧的数字。我目前无法弄清楚如何做到这一点。
我尝试使用各种形式的gsub
试图在排序之前删除数字而没有任何成功。
这是具有状态的数据集:
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"
答案
我们可以从字符向量中删除数字和点,然后使用order
仅对名称进行排序并对原始向量进行子集化。
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"
仅供参考,R具有内置状态名称,按升序存储在state.name
中
state.name
#[1] "Alabama" "Alaska" "Arizona" "Arkansas" "California" "Colorado"
#[7] "Connecticut" "Delaware" "Florida" "Georgia" "Hawaii" "Idaho"........
数据
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")
以上是关于如何通过删除名称开头的数字按字母顺序对数据进行排序[重复]的主要内容,如果未能解决你的问题,请参考以下文章