Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

split

r

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.

like image 370
Kaushik Avatar asked Oct 17 '25 10:10

Kaushik


1 Answers

I solved it with one line of code. Might look a bit naive for regex experts but for the sample data it works.

library(stringr)

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)

str_match(dat$Addresses,"(.+), (.+), (.+) (.+), (.+)")[ ,-1]
      [,1]                       [,2]          [,3] [,4]    [,5] 
[1,] "1626 Aviation Way"        "Albuquerque" "NM" "30906" "USA"
[2,] "1626 Aviation Way"        "Augusta"     "GA" "30906" "USA"
[3,] "325 Main St"              "Stratford"   "CT" "06615" "USA"
[4,] "4205 Bessie Coleman Blvd" "Tampa"       "FL" "33607" "USA"
like image 116
Alex Avatar answered Oct 19 '25 01:10

Alex