Split a address string into city, state and address in R

Viewed 3849

I have a address in a form of string given below:

dat = data.frame(Addresses = c("1626 Aviation Way, Albuquerque, NM 30906, USA", 
                               "1626 Aviation Way, Augusta, GA 30906, USA", 
                               "325 Main St, Stratford, CT 06615, USA", 
                               "4205 Bessie Coleman Blvd, Tampa, FL 33607, USA"), stringsAsFactors = FALSE)

I want to break it into 5 columns such as Street,City,State,Zip code, Postal. How can I do this in R.

3 Answers

Use my package tfwstring

Works automatically on any address type, even with prefixes and suffixes.

if (!require(remotes)) install.packages("remotes")
remotes::install_github("nbarsch/tfwstring")

To parse addresses:

tfwstring::parseaddress(address, check_python=TRUE, force_stateabb=FALSE, return="char")

On Mac and Linux python AND the python module usaddress should automatically install if missing (because unix is superior clearly).

On Windows it is recommended to

  1. Install python yourself from here: https://www.python.org/downloads/windows/
  2. Install c++ and python tools for visual studio: https://visualstudio.microsoft.com/downloads/
  3. Use powershell to pip3 install usaddress
  4. When running parseaddress() use check_python=FALSE to avoid issues running in the windows OS.
> dat = data.frame(Addresses = c("1626 Aviation Way, Albuquerque, NM 30906, USA", 
+                                "1626 Aviation Way, Augusta, GA 30906, USA", 
+                                "325 Main St, Stratford, CT 06615, USA", 
+                                "4205 Bessie Coleman Blvd, Tampa, FL 33607, USA"), stringsAsFactors = FALSE)


> parseaddress(dat$Addresses[1])
     AddressNumber         StreetName StreetNamePostType          PlaceName          StateName            ZipCode        CountryName 
            "1626"         "Aviation"              "Way"      "Albuquerque"               "NM"            "30906"              "USA" 
> parseaddress(dat$Addresses[2])
     AddressNumber         StreetName StreetNamePostType          PlaceName          StateName            ZipCode        CountryName 
            "1626"         "Aviation"              "Way"          "Augusta"               "GA"            "30906"              "USA" 
> parseaddress(dat$Addresses[3])
     AddressNumber         StreetName StreetNamePostType          PlaceName          StateName            ZipCode        CountryName 
             "325"             "Main"               "St"        "Stratford"               "CT"            "06615"              "USA" 
> parseaddress(dat$Addresses[4])
     AddressNumber         StreetName StreetNamePostType          PlaceName          StateName            ZipCode        CountryName 
            "4205"   "Bessie Coleman"             "Blvd"            "Tampa"               "FL"            "33607"              "USA" 
Related