tidyr unchop() in R: Expand Vector List Column Into Rows
The unchop() function in tidyr expands vector list-columns into multiple rows; each element of each vector becomes a row. It is the opposite of chop().
df |> unchop(col) # vectors to rows df |> unchop(c(col1, col2)) # multiple parallel cols df |> unchop(col, keep_empty = TRUE) # preserve empty cells df |> unnest_longer(col) # similar; alternative
Need explanation? Read on for examples and pitfalls.
What unchop() does in one sentence
unchop(data, cols, keep_empty = FALSE) expands a vector list-column into multiple rows; each element becomes its own row. Other columns are duplicated.
Syntax
unchop(data, cols, keep_empty = FALSE, ptype = NULL). cols are list columns to expand.
unchop and unnest_longer are nearly identical for vector list-cols. unchop is older; unnest_longer is the unified family.Five common patterns
1. Standard unchop
2. Multiple parallel list-cols
3. Keep empty cells
4. Round-trip
5. unnest_longer alternative
unchop and unnest_longer are functionally similar for vector list-cols.* unchop predates the unnest_ family; both are still maintained. For new code, the tidyr team recommends unnest_longer for consistency.unchop() vs unnest_longer() vs unnest()
| Function | Input | Output | Status |
|---|---|---|---|
unchop(col) |
Vector list-col | Rows | Active, older |
unnest_longer(col) |
Vector list-col | Rows | Active, newer (preferred) |
unnest(col) |
Tibble list-col | Rows | Active |
For vector list-cols, prefer unnest_longer for consistency with the rest of the family.
A practical workflow
Use unchop for round-tripping with chop().
Process each group's vectors; unchop back to long form.
Common pitfalls
Pitfall 1: empty cells dropped. Default keep_empty = FALSE. Pass TRUE to preserve as NA rows.
Pitfall 2: parallel list-cols must align. unchop(c(v, w)) requires v and w to have same length per cell.
unchop() is older than unnest_longer(); for new code, prefer unnest_longer. Both work; consistency favors unnest_longer.Try it yourself
Try it: Unchop a vector list-col and count rows. Save to ex_long.
Click to reveal solution
Explanation: unchop expands the vectors; total = 3 + 2 = 5 rows.
Related tidyr functions
After mastering unchop, look at:
chop(): opposite (rows to vector list-col)unnest_longer(): equivalent for vector list-colsunnest(): for tibble list-colsnest()/unnest(): tibble versions
FAQ
What does unchop do in tidyr?
unchop(data, col) expands a vector list-column into multiple rows. Each element becomes a row.
What is the difference between unchop and unnest_longer?
Functionally similar for vector list-cols. unchop is older; unnest_longer is part of the unified unnest family. Prefer unnest_longer for new code.
How do I keep empty cells with unchop?
Pass keep_empty = TRUE. Empty list cells become NA rows.
Is unchop deprecated?
No. It is still active. unnest_longer is preferred for consistency but both work.
How do I unchop multiple list-cols at once?
unchop(c(col1, col2)). Both must have the same vector length per cell.