pivot_longer & pivot_wider

See exactly where each value goes when you reshape a data frame

tidyr pivot_longer() pivot_wider()
Controls
Function
Collapse wide columns into rows
Which columns to pivot?
Blue columns will be collapsed into rows: Their names become values in a new column, and their numbers move to another new column. Click a blue column to exclude it.

Purple columns stay as-is. child_id and name can't be pivoted; they identify each child, so R needs them to repeat on every row. Measurement columns you exclude turn purple and stay as their own column in the result.
names_to
What should the new column containing old column names be called?
values_to
What should the new column of values be called?
names_prefix
Strip a repeated prefix from column names before putting them in names_to
values_drop_na
Drop rows where the value is NA?
Wide format (before)
pivot_longer()
Long format (after)
R Code
Color key
cols — columns being pivoted
names_to / names_from — column name destination
values_to / values_from — value destination
id columns — not pivoted, just repeated
advanced options