Handling Missing Values
in Reshaping
Learn how NA values appear, propagate, and can be controlled when you reshape data between wide and long formats in R.
What Are Missing Values in R?
In R, missing data is represented by the special value NA (Not Available). It signals that a measurement was not recorded, a response was skipped, or data simply does not exist for that combination of variables.
What Is Reshaping Data?
Reshaping transforms a data frame between two layouts without changing the underlying information:
Why NAs Matter in Reshaping
Reshaping and missing values interact in two important ways. Understanding both is critical to keeping your data clean and your analyses correct.
Core Rule: NAs in pivot_wider
When you pivot from long to wide, R builds a grid of every combination of row IDs and column names. If a combination has no matching row in the original data, the cell becomes NA.
Filling NAs with values_fill
The values_fill argument in pivot_wider() lets you replace any newly created NA with a default value of your choice:
values_fill vs values_drop_na
| values_fill | values_drop_na | |
|---|---|---|
| Primary use | pivot_wider() | pivot_longer() |
| Direction | Long → Wide | Wide → Long |
| What it does | Replaces NAs with a default value | Drops rows where the value is NA |
| Default | NULL (NAs stay) | FALSE (NAs kept) |
| Typical value | 0, "none" | TRUE |
Dropping NAs with pivot_longer
When pivoting from wide to long, any existing NA cells become rows in the output. Setting values_drop_na = TRUE silently removes them:
Step-by-Step Workflow
Example 1: Simple Wider Pivot
Students took quizzes in different subjects. Not every student took every quiz:
Example 1: Fix with values_fill
We can replace that NA with 0 to indicate "quiz not taken":
Example 2: Longer with Drop
Starting from the wide result (with NA), pivot back to long and drop the missing row:
Example 3: Named List Fill
When pivoting wider with multiple value columns, use a named list to set different defaults per column:
Decision Flowchart
Common Mistakes
Summary Table
| pivot_wider() | pivot_longer() | |
|---|---|---|
| Direction | Long → Wide | Wide → Long |
| NA behavior | Creates NAs for missing combos | Preserves existing NAs as rows |
| Control argument | values_fill | values_drop_na |
| Accepts | Scalar or named list | TRUE / FALSE |
| Effect | Replace NA with fill value | Remove NA rows from output |
| Pre-existing NAs | Also replaced if values_fill is set | Also dropped if TRUE |
Question 1 of 5
When you use pivot_wider() and a combination of row ID and column name has no matching observation, what happens to that cell?
Question 2 of 5
Which argument would you use to replace newly created NAs with 0 when pivoting wider?
Question 3 of 5
You have a wide table with some NA values. You pivot it longer using pivot_longer() with values_drop_na = TRUE. What happens?
Question 4 of 5
You want to fill NAs in a price column with 0.00 and in a qty column with 0L when pivoting wider. Which syntax is correct?
Question 5 of 5
Your original long data already contains 2 NA values in the score column. After pivot_wider(), the wide table has 5 NAs. How many NAs were created by the reshape?