Replacements for gather() and spread()

March 27, 2019

Motivation and goals

Today’s lesson will follow the pivoting vignette from the tidyr package. Last semester we introduced the functions and gather() and spread() for reshaping data back and forth between wide and long format.

We made the point that these were hard functions to learn and that they were notorious for being confusing. Well, Hadley Wickham recently announced a new strategy for reshaping data with “pivot” functions.

You may have heard a rumour that gather/spread are going away. This is simply not true (they’ll stay around forever) but I am working on better replacements which you can learn about at Now is a great time for feedback! #rstats

— Hadley Wickham (@hadleywickham) March 19, 2019

They’re still in development, but I think everyone agrees that

Pivot functions are soooooo much better

— Hadley Wickham (@hadleywickham) March 19, 2019

However, the pivot functions are not in the CRAN release of the tidyr package yet, so you won’t see them show up if you run library(tidyr) or library(tidyverse). Let’s go over some ways to install packages before we look at reshaping data.

Methods for installing R packages

Install a package from CRAN (the main repository of R packages):


There are a lot of helpful functions for dealing with packages in the devtools package. Install that package if you haven’t already, and then load it:


When an update to a package is pushed to CRAN, you can install the update using:


If you find youself dealing with multiple version of packages, or want to make sure your code will work down the line even if the packages change, look into using packrat. Sometimes you want to revert to an other version of a package if something in the source code has changed. In this case you can look in the archive on CRAN, e.g., under the “Old sources” section.

Then you can find the version number corresponding to the date you’re looking for. You can either download it directly, or install with:

install_version("tidyr", version = "0.7.0") # for example

But what if you want a new version, not an old version? The development version of many packages can be found on github, e.g. You can get the most up-to-date features (with the caveat that they may not be fully tested) by downlading from github:


So fun that last one to get access to the pivot_longer() and pivot_wider() functions before they’re released on CRAN, and then follow along with the vignette at