Ank
Ank

Reputation: 1904

How to split address column in R

I have an address column in a dataframe like below:

Address
101 Marietta Street NorthWest Atlanta GA 30303

Now I want to split it into 4 diff columns like

Address                          City        State     Zip
101 Marietta Street NorthWest    Atlanta     GA        30303

It is guaranteed that the last value in address column will be zip code, second last will be state, third last will be city and remaining will be address. So I am thinking, I can split address column values with space and extract values from rear.

How can I do this?

Upvotes: 1

Views: 941

Answers (1)

Ronak Shah
Ronak Shah

Reputation: 389047

We can use tidyr::extract to get last 3 words in separate columns and remaining text as Address

tidyr::extract(df, Address, c("Address", "City", "State", "Zip"), 
                   regex = "(.+) (\\w+) (\\w+) (\\w+)")

#                        Address    City State   Zip
#1 101 Marietta Street NorthWest Atlanta    GA 30303

Upvotes: 1

Related Questions