Reshaping data with tidyr

Finally, we get to turn our attention to the other staple of the tidyverse, tidyr.

Though this package offers more functionality, the main purpose of this package is to reshape data (convert from long to wide format) in a tidy manner.

Let’s recreate long, a long format that contains the play counts for each year/month, using the following code:

> long <- tracks %>%+   group_by(theyear=year(thedate), themonth) %>%+   summarise(N=n())> long# A tibble: 107 x 3# Groups: theyear [?]   theyear themonth     N     <dbl> <ord>    <int> 1    2008 Jan        877 2    2008 Feb        984 3    2008 Mar       1486 4    2008 Apr       1101...# ... with 97 more rows

Now let’s get this into wide format with the different month in its own columns.

The tidyr equivalent of the dcast function ...

Get Data Analysis with R - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.