I have a large file with a variable state
that has full state names. I would like to replace it with the state abbreviations (that is "NY" for "New York"). Is there an easy way to do this (apart from using several if-else commands)? May be using replace()
statement?
To make room for the ZIP Code, state names needed to be abbreviated. The Department provided an initial list of abbreviations in June 1963, but many had three or four letters, which was still too long. In October 1963, the Department settled on the current two-letter abbreviations.
R has two built-in constants that might help: state.abb
with the abbreviations, and state.name
with the full names. Here is a simple usage example:
> x <- c("New York", "Virginia") > state.abb[match(x,state.name)] [1] "NY" "VA"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With