State Name & Abbreviation in R (2 Examples)

 

This tutorial demonstrates how to convert a USA state name to abbreviation and the other way around in the R programming language.

The article contains the following topics:

Let’s jump right to the examples.

 

state.abb & state.name Data Objects

An essential aspect for this tutorial are the state.abb and state.name data objects.

These two data objects contain all abbreviations and state names of the fifty USA states, and they are already provided by the basic installation of the R programming language!

Let’s have a look at the state.abb data object:

state.abb                                    # Print state abbreviations
#  [1] "AL" "AK" "AZ" "AR" "CA" "CO" "CT" "DE" "FL" "GA" "HI" "ID" "IL" "IN" "IA"
# [16] "KS" "KY" "LA" "ME" "MD" "MA" "MI" "MN" "MS" "MO" "MT" "NE" "NV" "NH" "NJ"
# [31] "NM" "NY" "NC" "ND" "OH" "OK" "OR" "PA" "RI" "SC" "SD" "TN" "TX" "UT" "VT"
# [46] "VA" "WA" "WV" "WI" "WY"

As you can see, it contains all state abbreviations in alphabetical order of the corresponding state names.

Similar to this, we can have a look at the state.name object:

state.name                                   # Print state names
#  [1] "Alabama"        "Alaska"         "Arizona"        "Arkansas"      
#  [5] "California"     "Colorado"       "Connecticut"    "Delaware"      
#  [9] "Florida"        "Georgia"        "Hawaii"         "Idaho"         
# [13] "Illinois"       "Indiana"        "Iowa"           "Kansas"        
# [17] "Kentucky"       "Louisiana"      "Maine"          "Maryland"      
# [21] "Massachusetts"  "Michigan"       "Minnesota"      "Mississippi"   
# [25] "Missouri"       "Montana"        "Nebraska"       "Nevada"        
# [29] "New Hampshire"  "New Jersey"     "New Mexico"     "New York"      
# [33] "North Carolina" "North Dakota"   "Ohio"           "Oklahoma"      
# [37] "Oregon"         "Pennsylvania"   "Rhode Island"   "South Carolina"
# [41] "South Dakota"   "Tennessee"      "Texas"          "Utah"          
# [45] "Vermont"        "Virginia"       "Washington"     "West Virginia" 
# [49] "Wisconsin"      "Wyoming"

It contains the entire state names in the same order as the state abbreviations.

We can use these data objects to perform certain matching operations and data manipulations.

Below, you can find two examples for this!

 

Example 1: Convert State Name to State Abbreviation Using grep() Function

The following R programming syntax shows how to get the corresponding abbreviation of a US American state name.

For this task, we can apply the grep function as shown below:

state.abb[grep("New Jersey", state.name)]    # Get state abbreviation
# [1] "NJ"

With the previous R code, we have returned the state abbreviation of the state New Jersey (i.e. NJ).

 

Example 2: Convert State Abbreviation to State Name Using grep() Function

We can do the same as in Example 1, but the other way around.

The following R code illustrates how to return the state name that corresponds to a state abbreviation:

state.name[grep("NJ", state.abb)]            # Get state name
# [1] "New Jersey"

NJ becomes New Jersey – looks good!

 

Video & Further Resources

I have recently released a video on my YouTube channel, which illustrates the R code of this post. You can find the video below.

 

Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.

YouTube Content Consent Button Thumbnail

YouTube privacy policy

If you accept this notice, your choice will be saved and the page will refresh.

 

Furthermore, you might want to have a look at the related tutorials on my website. You can find some tutorials below:

 

Summary: This article has explained how to switch from a USA state name to abbreviation and the other way around in the R programming language. If you have any further questions or comments, let me know in the comments below.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


2 Comments. Leave new

Leave a Reply

Your email address will not be published.

Fill out this field
Fill out this field
Please enter a valid email address.

Menu
Top