Data shape

tidyr provides three families of tools for changing the form of a dataset, while preserving its values:

  • Pivotting, which changes the representation of a rectangular dataset. See vignette("pivot") for more details.
  • Rectangling, which turns deeply nested lists into tidy tibbles. See vignette("rectangle") for more details.
  • Nesting and unnesting, which support converstaion to and from an alternative representation of grouped data where a group becomes a single row containing a nested data frame. See vignette("nest") for more details.

pivot_longer() pivot_longer_spec()

Pivot data from wide to long

pivot_wider() pivot_wider_spec()

Pivot data from long to wide

nest() unnest()

Nest and unnest

hoist() unnest_longer() unnest_wider() unnest_auto()

Rectangle a nested list into a tidy tibble

spread()

Spread a key-value pair across multiple columns

gather()

Gather columns into key-value pairs

Character vectors

Multiple variables are sometimes pasted together into a single column, and these tools help you separate back out into individual columns.

extract()

Extract a character column into multiple columns using regular expression groups

separate()

Separate a character column into multiple columns using a regular expression separator

separate_rows()

Separate a collapsed column into multiple rows

unite()

Unite multiple columns into one by pasting strings together

Missing values

Tools for converting between implicit (absent rows) and explicit (NA) missing values, and for handling explicit NAs.

complete()

Complete a data frame with missing combinations of data

drop_na()

Drop rows containing missing values

expand() crossing() nesting()

Expand data frame to include all combinations of values

expand_grid()

Create a tibble from all combinations of inputs

fill()

Fill in missing values with previous or next value

full_seq()

Create the full sequence of values in a vector

replace_na()

Replace missing values

Miscellanea

chop() unchop()

Chop and unchop

pack() unpack()

Pack and unpack

uncount()

"Uncount" a data frame

Data

billboard

Song rankings for billboard top 100 in the year 2000

construction

Completed construction in the US in 2018

fish_encounters

Fish encounters

relig_income

Pew religion and income survey

smiths

Some data about the Smith family

table1 table2 table3 table4a table4b table5

Example tabular representations

us_rent_income

US rent and income data

who population

World Health Organization TB data

world_bank_pop

Population data from the world bank