Press "Enter" to skip to content

Unpivoting Data in R

Steven Sanderson shows off a function with a slightly confusing name:

In the world of data analysis and manipulation, tidying and reshaping data is often an essential step. R’s tidyr library provides powerful tools to efficiently transform and reshape data. One such function is pivot_longer(). In this blog post, we’ll explore how pivot_longer() works and demonstrate its usage through several examples. By the end, you’ll have a solid understanding of how to use this function to make your data more manageable and insightful.

The slight confusion is that this function is really unpivoting rather than pivoting. In R terminology, a pivot takes you from longer data to wider data: it uses some function to convert data from N rows * M columns to n*m, where N > n and M < m. Unpivoting does the opposite and I personally like that terminology better than “pivot wider” or “pivot longer.”