Save up to $499! Grab all Python courses for $49 or all online courses we’ve ever launched for only $169. Only Feb 11-16. Happy Valentine's!
Changing data format
11. Spread with new column names


Awesome! But suppose we wanted to create column names based on certain values? For example, say we wanted to have a column for each year in our population dataset, and we wanted it named year_<year name>. This column name should increase with each year: year_2009, year_2010 and so on.

To do so, we'd simply use the sep argument with spread(). This is what it looks like:

spread(population, year, pop, sep = "_")

This creates new column names based on the original column name (year) and levels made up from values in that column (here, the various years). This will give us columns named year_2008, year_2009, etc.


Use the spread() function on unemployment2 to spread the column year with values from rate_u.
Add the argument sep = "_" to create column names as shown in the instructions (e.g., year_*).

Stuck? Here's a hint!


spread(unemployment2, year, rate_u, sep = "_")