How to get the US states name abbreviation in R?

R ProgrammingServer Side ProgrammingProgramming

There are fifty states in United States, few of them have short names but most of the states have a lengthy name. Therefore, if we are dealing with data that has states name of United States then it will be a little complicated to access the states by using their name, hence it is preferred to use abbreviation. We can get the state name abbreviation with the help of state.abb function.

Examples

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"
raja
Updated on 24-Aug-2020 12:07:43

Advertisements