user6794408 user6794408 - 29 days ago 4x
R Question

How to create a new column from a data frame, but using on a piece of data from existing column

Basically, I have a data frame called "top100_retailers". It has a column named "Headquarter" that contains the city, state(For example: "Bentonville, Ark."). I want to create 2 new columns called "City" and "State". How do I only take the city information and assign it to the new "City" column and only take the state information and assign it to the new "State" column?


We can do this with separate from tidyr

top100_retailers <- separate(top100_retailers, Headquarter, into = c("City", "State"))

If we want to keep the 'Headquarter' column as well, use the remove = FALSE argument as it is by default TRUE.