R
DOCUMENT

R tidyr Data-Wrangling Cheat Sheet

A practical tidyr reference for long and wide tables, splitting and combining fields, missing values, nested data and column names.

Version 2026-08-22通用Public reference material; verify the included notice and original project terms before redistribution

What this reference covers

tidyr operations are useful when the table structure should match the analysis question. This reference covers long and wide reshaping, splitting and combining columns, missing-value handling, nested data and naming conventions.

The important question is what one row, one key and one measurement mean. Long tables often fit grouping and plotting, while wide tables may fit a matrix-like report or interface. A one-to-many key can legitimately expand rows, but an accidental duplicate join can look the same, so expected grain and row counts belong in the project notes.

A validation-first workflow

Define the input grain and target structure, normalize names and types, perform one pivot or split, and check row counts, key uniqueness, measurement types and known aggregates after each step. Keep missing, empty and unknown values distinct.

Maintenance note

This sheet is a structural-operations reference; it does not infer field meaning, missingness or the correct statistical method. Business owners should review the result, especially for financial, health or location data. Content review date: 2026-08-23.

SAVE TO CLOUD

Save to your cloud drive

Save the complete collection first so files remain together and are easier to access across devices.

Links checked 2026-08-06
Save first, access when you need itOn desktop, scan with the matching cloud-drive app. On mobile, tap the save button.
GUIDE

R tidyr data-wrangling study guide

Define the row grain and key, choose one structural operation on a small sample and compare row counts, uniqueness and known aggregates before scaling up.

Before you start

  • Know R data frames, filtering, joins and grouping.
  • Prepare a small sanitized sample with duplicate keys, missing values and multi-value fields.
  • Write the current and target table structure before choosing a function.
02

Quick start

  1. 01

    Define what one row means

    Record the entity, time grain, primary key, candidate keys and measurement columns represented by each row.

  2. 02

    Normalize names and types

    Align column names, dates, numbers and strings, and remove obvious whitespace or duplicate-name ambiguity.

  3. 03

    Reshape long and wide forms

    Choose the form required by the next analysis or plot, then check row count, key uniqueness and measurement types.

  4. 04

    Review missing and duplicate values

    Separate true, structural and unknown values, inspect duplicate keys and only then fill, drop or aggregate records.

Usage tips

  • Long tables often suit grouping, plotting and batch operations, while wide tables fit some reports and matrix interfaces.
  • Record keys and expected rows for every pivot or split so a silent expansion is visible.
  • Keep missing values, empty strings and a literal value such as unknown separate in the data dictionary.
Troubleshooting and uninstall

Why did pivoting report duplicate combinations?

Check whether the row-identifying keys are unique, aggregate duplicate records or add the missing key, then reshape again.

Why did results change after filling missing values?

Separate structural and random missingness, retain an imputation flag and compare distributions and grouped totals before and after.

FAQ

Frequently asked questions

Why define row meaning before data wrangling?

Grain and keys determine duplicates, long-wide structure and aggregation, preventing the statistical unit from changing silently.

When is a long table useful?

Grouped analysis, plotting and repeated operations often fit long form, while wide form may be needed by a specific report or matrix interface.

Are missing values and empty strings equivalent?

No. A missing value may mean unknown or unobserved, while an empty string may mean an empty input; the field definition decides the treatment.