01 / 21
A B C 1 NA 3 NA 5 6

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.

R Programming

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.

NA
The universal missing-value sentinel in R. It is not the same as 0 or an empty string.
is.na()
The function that tests whether a value is NA. Returns TRUE or FALSE for each element.

What Is Reshaping Data?

Reshaping transforms a data frame between two layouts without changing the underlying information:

Wide Format
Each variable gets its own column. One row per subject. Compact but rigid.
Long Format
One observation per row. A "key" column names the variable, a "value" column holds the measurement.
WIDE name math sci Ana 90 NA Ben 85 92 Cat NA 78 reshape LONG name subj score Ana math 90 Ana sci NA Ben math 85 Ben sci 92 Cat math NA Cat sci 78

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.

NAs Created
Pivoting to wide format may introduce new NA cells when a subject has no observation for a particular variable.
NAs Removed
Pivoting to long format lets you drop NA rows, producing a compact dataset with no missing values.

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.

Long Input id key val 1 a 10 1 b 20 2 a 30 3 b 50 id=2,b missing id=3,a missing pivot_wider Wide Output id a b 1 10 20 2 30 NA 3 NA 50

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:

pivot_wider(data, names_from = key, values_from = val, values_fill = 0 )

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:

pivot_longer(data, cols = c(math, sci), names_to = "subject", values_to = "score", values_drop_na = TRUE )
Without drop
All 6 rows kept, including 2 with NA scores.
With drop
Only 4 rows with actual scores remain. Clean and analysis-ready.

Step-by-Step Workflow

1
Inspect your data. Run sum(is.na(df)) to count existing NAs before any reshape.
2
Decide on direction. Are you going wide (pivot_wider) or long (pivot_longer)?
3
Choose your NA strategy. Fill with a default? Drop the rows? Keep them?
4
Set the argument. Use values_fill for wider or values_drop_na for longer.
5
Verify. Run sum(is.na(result)) again to confirm NAs are handled as expected.

Example 1: Simple Wider Pivot

Students took quizzes in different subjects. Not every student took every quiz:

# Starting data (long) scores <- tibble( student = c("Amy", "Amy", "Bob"), quiz = c("math", "art", "math"), score = c(95, 88, 72) ) # Pivot wider: Bob has no art score scores |> pivot_wider( names_from = quiz, values_from = score )

Example 1: Fix with values_fill

We can replace that NA with 0 to indicate "quiz not taken":

scores |> pivot_wider( names_from = quiz, values_from = score, values_fill = 0 ) # Result: # student math art # Amy 95 88 # Bob 72 0 ← was NA

Example 2: Longer with Drop

Starting from the wide result (with NA), pivot back to long and drop the missing row:

wide_scores <- tibble( student = c("Amy", "Bob"), math = c(95, 72), art = c(88, NA) ) wide_scores |> pivot_longer( cols = c(math, art), names_to = "quiz", values_to = "score", values_drop_na = TRUE ) # Result: 3 rows (Bob/art dropped)

Example 3: Named List Fill

When pivoting wider with multiple value columns, use a named list to set different defaults per column:

df |> pivot_wider( names_from = item, values_from = c(price, qty), values_fill = list( price = 0.00, qty = 0L ) )

Decision Flowchart

Reshaping data? Which direction? wider longer wider Fill NAs? with default? yes values_fill = X longer Drop NA rows? yes values_drop_na = TRUE

Common Mistakes

✕ Confusing the Arguments
Mixing up values_fill (replaces NAs with a default) and values_drop_na (removes NA rows). Remember: values_drop_na is for pivot_longer() only.
✕ Type Mismatch
Filling a numeric column with a string like "missing". The fill value must match the column type.
✕ Ignoring Pre-existing NAs
Assuming all NAs come from reshaping. Some may have been in the original data. Always inspect first.

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?