See exactly where each value goes when you reshape a data frame
tidyrpivot_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?
names_from
Which column's values should become new column headers?
values_from
Which column holds the values to fill those new columns?
names_prefix
Add a prefix to the new column names. Bare numbers (like 6, 7, 8) make awkward column names in R; a prefix fixes that.
⚠ Without a prefix, column names will be bare numbers. In R, you'd need backticks to use them: df$`6`