From tidyr 0.8.3.9000
, a new function called pivot_wider()
is introduced. It is basically an upgraded version of the previous spread()
function (which is, moreover, no longer under active development). From pivoting vignette:
This vignette describes the use of the new pivot_longer() and
pivot_wider() functions. Their goal is to improve the usability of
gather() and spread(), and incorporate state-of-the-art features found
in other packages.
For some time, it’s been obvious that there is something fundamentally
wrong with the design of spread() and gather(). Many people don’t find
the names intuitive and find it hard to remember which direction
corresponds to spreading and which to gathering. It also seems
surprisingly hard to remember the arguments to these functions,
meaning that many people (including me!) have to consult the
documentation every time.
How to use it (using the data from @Aaron):
pivot_wider(data = tmp, names_from = y, values_from = z)
x a b c
1 x 1 2 3
2 y 3 3 2
Or in a "full" tidyverse
fashion:
tmp %>%
pivot_wider(names_from = y, values_from = z)