Skip to content

Pivot Longer

Reshape

Stack multiple columns into key-value rows, converting wide-format data into long-format so that each observation occupies its own row. Long format is the standard layout expected by most statistical modelling functions, mixed-effects models, and many plotting libraries.

When to Use

  • Each measurement lives in a separate column (e.g., Day1, Day2, Day3) and you need them stacked into a single column for analysis. Most repeated-measures and mixed-effects models require long format.
  • You are preparing data for repeated-measures tests, mixed-effects models, or plotting tools (like ggplot2 or seaborn) that expect one observation per row.
  • Column headers contain variable values (time points, doses, conditions) that should be recorded as data values in their own column, not as structural column names.
  • You need to combine several measurement columns into a single column to calculate summary statistics across all measurements uniformly.

Required Inputs

  • Columns to Pivot -- the columns to stack into rows (e.g., Day1, Day2, Day3). Select all the columns that represent the same type of measurement taken under different conditions.
  • Names To -- the name for the new column that will receive the original column headers (e.g., "Day" or "TimePoint"). This column records which original column each value came from.
  • Values To -- the name for the new column that will receive the cell values (e.g., "Measurement" or "Score"). This column holds the actual data values.

What Changes

  • Row count increases because each pivoted column generates one new row per original row. If you pivot 3 columns, each original row becomes 3 rows in the result.
  • Column count decreases because all pivoted columns merge into exactly two new columns: one for the names and one for the values.
  • Non-pivoted columns (ID columns such as Subject or PatientID) are repeated for each new row to maintain the relationship between observations and their source entity.
  • The total number of data values is preserved -- the same information is simply rearranged from a wide layout into a tall layout.

Common Pitfalls

  • Pivoting columns with mixed data types (numeric and text) can cause unexpected string conversions in the values column. Ensure all columns being pivoted contain the same type of data.
  • Forgetting to exclude ID columns from the pivot selection will stack IDs alongside measurements, corrupting the data structure. Only select the measurement columns you want to stack.
  • Very wide datasets with many columns can produce a very large number of rows after pivoting (original rows x pivoted columns), potentially slowing the interface for very large files.
  • If the original wide-format data has missing values in some columns, these become explicit missing value rows in the long format. Decide whether to keep or drop them after pivoting.

Example: Before & After

Before
SubjectPrePost
S18092
S27588
After
SubjectTimeScore
S1Pre80
S1Post92
S2Pre75
S2Post88

Columns to Pivot: Pre, Post. Names To: Time, Values To: Score. Each subject's single row expands into two rows, one per time point.