Tidyr Cheat Sheet

In R

Tidyr is a popular R package that helps users to tidy and reshape data. It is a part of the tidyverse, a collection of R packages designed for data science. Tidyr provides a set of functions that make it easy to transform messy data into a tidy format that is easier to work with.

The package is particularly useful for data cleaning and preparation tasks. It allows users to split, combine, and reshape data in a variety of ways. Tidyr also provides tools for dealing with missing data, which is a common problem in real-world datasets.

This cheat sheet provides an overview of the tidyr package. Enjoy!

Gathering and Spreading Data

FunctionDescription
gather()Converts wide data to long format by gathering columns into rows.
spread()Converts long data to wide format by spreading rows into columns.
pivot_longer()Converts wide data to long format by gathering columns into rows, with more flexibility than gather().
pivot_wider()Converts long data to wide format by spreading rows into columns, with more flexibility than spread().

Separating and Uniting Data

FunctionDescription
separate()Separates a single column into multiple columns based on a separator.
unite()Combines multiple columns into a single column with a separator.

Completing and Replacing Data

FunctionDescription
complete()Completes missing combinations of data, filling in missing values with NA or a specified value.
replace_na()Replaces missing values with NA or a specified value.

References

# #