Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

State name to abbreviation

Tags:

r

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?

like image 586
user227290 Avatar asked Mar 23 '11 21:03

user227290


People also ask

When did states go to 2 letter abbreviations?

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.


1 Answers

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" 
like image 98
Aniko Avatar answered Oct 15 '22 18:10

Aniko