如何在 R 中获取美国州名称缩写?


美利坚合众国有五十个州,其中几个州的名称较短,但大多数州的名称较长。因此,如果我们处理的数据包含美利坚合众国的州名称,那么使用其名称访问州将有点复杂,因此最好使用缩写。我们可以借助 state.abb 函数来获取州名称缩写。

示例

state.abb[which(state.name=="New York")]
[1] "NY"
state.abb[which(state.name=="California")]
[1] "CA"
state.abb[which(state.name=="Texas")]
[1] "TX"
state.abb[which(state.name=="Florida")]
[1] "FL"
state.abb[which(state.name=="Washington")]
[1] "WA"
state.abb[which(state.name=="Michigan")]
[1] "MI"
state.abb[which(state.name=="New Jersey")]
[1] "NJ"
state.abb[which(state.name=="Arizona")]
[1] "AZ"
state.abb[which(state.name=="Pennsylvania")]
[1] "PA"
state.abb[which(state.name=="Alaska")]
[1] "AK"
state.abb[which(state.name=="Massachusetts")]
[1] "MA"
state.abb[which(state.name=="Ohio")]
[1] "OH"
state.abb[which(state.name=="Georgia")]
[1] "GA"
state.abb[which(state.name=="Minnesota")]
[1] "MN"
state.abb[which(state.name=="Hawaii")]
[1] "HI"
state.abb[which(state.name=="Illinois")]
[1] "IL"
state.abb[which(state.name=="Colorado")]
[1] "CO"
state.abb[which(state.name=="North Carolina")]
[1] "NC"
state.abb[which(state.name=="Virginia")]
[1] "VA"
state.abb[which(state.name=="Alabama")]
[1] "AL"
state.abb[which(state.name=="Oregon")]
[1] "OR"
state.abb[which(state.name=="Indiana")]
[1] "IN"
state.abb[which(state.name=="Maryland")]
[1] "MD"
state.abb[which(state.name=="Missouri")]
[1] "MO"
state.abb[which(state.name=="Tennessee")]
[1] "TN"
state.abb[which(state.name=="Wisconsin")]
[1] "WI"
state.abb[which(state.name=="Montana")]
[1] "MT"
state.abb[which(state.name=="South Carolina")]
[1] "SC"
state.abb[which(state.name=="Maine")]
[1] "ME"
state.abb[which(state.name=="Utah")]
[1] "UT"
state.abb[which(state.name=="Connecticut")]
[1] "CT"
state.abb[which(state.name=="Mississippi")]
[1] "MS"
state.abb[which(state.name=="Wyoming")]
[1] "WY"
state.abb[which(state.name=="Iowa")]
[1] "IA"
state.abb[which(state.name=="Louisiana")]
[1] "LA"
state.abb[which(state.name=="Kentucky")]
[1] "KY"
state.abb[which(state.name=="Nevada")]
[1] "NV"
state.abb[which(state.name=="Rhode Island")]
[1] "RI"
state.abb[which(state.name=="Arkansas")]
[1] "AR"
state.abb[which(state.name=="New Mexico")]
[1] "NM"
state.abb[which(state.name=="Idaho")]
[1] "ID"
state.abb[which(state.name=="Delaware")]
[1] "DE"
state.abb[which(state.name=="Nebraska")]
[1] "NE"
state.abb[which(state.name=="Kansas")]
[1] "KS"
state.abb[which(state.name=="Vermont")]
[1] "VT"
state.abb[which(state.name=="New Hampshire")]
[1] "NH"
state.abb[which(state.name=="South Dakota")]
[1] "SD"
state.abb[which(state.name=="Oklahoma")]
[1] "OK"
state.abb[which(state.name=="West Virginia")]
[1] "WV"
state.abb[which(state.name=="North Dakota")]
[1] "ND"

更新于: 2020 年 8 月 24 日

399 次观看

开启你的 职业生涯

通过完成课程获得认证

开始
广告