Skip to content

tidyverse/tidyr

This branch is up to date with main.

Folders and files

NameName
Last commit message
Last commit date

Latest commit

1c97084 · Mar 3, 2025
Aug 5, 2024
Mar 3, 2025
Feb 21, 2025
Feb 21, 2025
Jan 13, 2023
Aug 28, 2024
Nov 1, 2023
Jan 23, 2023
Nov 15, 2021
Feb 21, 2025
Oct 24, 2024
Mar 3, 2025
Mar 21, 2019
Aug 15, 2024
Nov 1, 2023
Nov 1, 2023
Jan 10, 2023
Aug 28, 2024
Aug 5, 2024
Aug 5, 2024
Jan 22, 2024
Feb 21, 2025
Mar 19, 2020
Jan 22, 2024
Jun 21, 2022

Repository files navigation

tidyr tidyr website

CRAN status R-CMD-check Codecov test coverage

Overview

The goal of tidyr is to help you create tidy data. Tidy data is data where:

  1. Each variable is a column; each column is a variable.
  2. Each observation is a row; each row is an observation.
  3. Each value is a cell; each cell is a single value.

Tidy data describes a standard way of storing data that is used wherever possible throughout the tidyverse. If you ensure that your data is tidy, you’ll spend less time fighting with the tools and more time working on your analysis. Learn more about tidy data in vignette("tidy-data").

Installation

# The easiest way to get tidyr is to install the whole tidyverse:
install.packages("tidyverse")

# Alternatively, install just tidyr:
install.packages("tidyr")

# Or the development version from GitHub:
# install.packages("pak")
pak::pak("tidyverse/tidyr")

Cheatsheet

Getting started

library(tidyr)

tidyr functions fall into five main categories:

  • “Pivoting” which converts between long and wide forms. tidyr 1.0.0 introduces pivot_longer() and pivot_wider(), replacing the older spread() and gather() functions. See vignette("pivot") for more details.

  • “Rectangling”, which turns deeply nested lists (as from JSON) into tidy tibbles. See unnest_longer(), unnest_wider(), hoist(), and vignette("rectangle") for more details.

  • Nesting converts grouped data to a form where each group becomes a single row containing a nested data frame, and unnesting does the opposite. See nest(), unnest(), and vignette("nest") for more details.

  • Splitting and combining character columns. Use separate_wider_delim(), separate_wider_position(), and separate_wider_regex() to pull a single character column into multiple columns; use unite() to combine multiple columns into a single character column.

  • Make implicit missing values explicit with complete(); make explicit missing values implicit with drop_na(); replace missing values with next/previous value with fill(), or a known value with replace_na().

Related work

tidyr supersedes reshape2 (2010-2014) and reshape (2005-2010). Somewhat counterintuitively, each iteration of the package has done less. tidyr is designed specifically for tidying data, not general reshaping (reshape2), or the general aggregation (reshape).

data.table provides high-performance implementations of melt() and dcast()

If you’d like to read more about data reshaping from a CS perspective, I’d recommend the following three papers:

To guide your reading, here’s a translation between the terminology used in different places:

tidyr 1.0.0 pivot longer pivot wider
tidyr < 1.0.0 gather spread
reshape(2) melt cast
spreadsheets unpivot pivot
databases fold unfold

Getting help

If you encounter a clear bug, please file a minimal reproducible example on github. For questions and other discussion, please use forum.posit.co.


Please note that the tidyr project is released with a Contributor Code of Conduct. By contributing to this project, you agree to abide by its terms.