user6794408 user6794408 - 1 year ago 59
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?

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download