Pivot Tables
pivot — wide format
Pivot a long table into wide format by spreading a column's values across multiple columns.
Minimal pivot
This creates a table where each unique value of region becomes a column, and each row is a product, with sum(amount) as the values.
Multiple aggregations
Multiple row and column dimensions
Options
| Option | Description |
|---|---|
agg <fn> <col> |
Aggregation function and value column |
rows <cols> |
Columns to use as row index |
cols <cols> |
Column whose values become column headers |
unpivot — long format
Convert wide-format data (multiple value columns) to long format (single value column with a variable identifier).
Basic unpivot
Specify the ID column(s) to keep fixed:
All other columns become rows, with a generated variable and value column.
Specify value columns
Columns not listed in id or cols are dropped.
Custom column names
Multiple ID columns
| Option | Description |
|---|---|
id <cols> |
Columns to keep as identifiers (required) |
cols <cols> |
Columns to unpivot (optional; default = all non-id columns) |
variable "name" |
Name for the variable column (default "variable") |
value "name" |
Name for the value column (default "value") |