0

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?

esote
  • 831
  • 12
  • 25

1 Answers1

0

We can do this with separate from tidyr

library(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.

akrun
  • 874,273
  • 37
  • 540
  • 662
  • Should this automatically create the new columns "City" and "State" for the data frame? – user6794408 Sep 26 '16 at 03:28
  • @user6794408 The `separate` will automaticaly find the delimiter, in this case the `,` and create the two columns. We have to provide the names of the new columns i.e. `City` and `State` – akrun Sep 26 '16 at 03:31