Learn R Programming

tidyr

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.0pivot longerpivot wider
tidyr < 1.0.0gatherspread
reshape(2)meltcast
spreadsheetsunpivotpivot
databasesfoldunfold

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.

Copy Link

Version

Install

install.packages('tidyr')

Monthly Downloads

1,492,589

Version

1.3.2

License

MIT + file LICENSE

Issues

Pull Requests

Stars

Forks

Maintainer

Hadley Wickham

Last Published

December 19th, 2025

Functions in tidyr (1.3.2)

expand_grid

Create a tibble from all combinations of inputs
chop

Chop and unchop
fill

Fill in missing values with previous or next value
full_seq

Create the full sequence of values in a vector
nest

Nest rows into a list-column of data frames
cms_patient_experience

Data from the Centers for Medicare & Medicaid Services
hoist

Hoist values out of list-columns
household

Household data
billboard

Song rankings for Billboard top 100 in the year 2000
check_pivot_spec

Check assumptions about a pivot spec
fish_encounters

Fish encounters
pack

Pack and unpack
%>%

Pipe operator
gather

Gather columns into key-value pairs
pivot_longer_spec

Pivot data from wide to long using a spec
pivot_longer

Pivot data from wide to long
pivot_wider_spec

Pivot data from long to wide using a spec
pivot_wider

Pivot data from long to wide
complete

Complete a data frame with missing combinations of data
construction

Completed construction in the US in 2018
extract

Extract a character column into multiple columns using regular expression groups
replace_na

Replace NAs with specified values
separate

Separate a character column into multiple columns with a regular expression or numeric locations
nest_legacy

Legacy versions of nest() and unnest()
separate_wider_delim

Split a string into columns
unite

Unite multiple columns into one by pasting strings together
uncount

"Uncount" a data frame
separate_rows

Separate a collapsed column into multiple rows
separate_longer_delim

Split a string into rows
world_bank_pop

Population data from the World Bank
smiths

Some data about the Smith family
extract_numeric

Extract numeric component of variable.
tidyr-package

tidyr: Tidy Messy Data
reexports

Objects exported from other packages
relig_income

Pew religion and income survey
tidyr_data_masking

Argument type: data-masking
unnest_longer

Unnest a list-column into rows
unnest_wider

Unnest a list-column into columns
spread

Spread a key-value pair across multiple columns
table1

Example tabular representations
unnest

Unnest a list-column of data frames into rows and columns
unnest_auto

Automatically call unnest_wider() or unnest_longer()
tidyr_tidy_select

Argument type: tidy-select
tidyr_legacy

Legacy name repair
us_rent_income

US rent and income data
who

World Health Organization TB data
deprecated-se

Deprecated SE versions of main verbs
expand

Expand data frame to include all possible combinations of values
drop_na

Drop rows containing missing values