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.
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"
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