unstack
Reshapes a table from long to wide format.
Category: Language Fundamentals > Data Types
Syntax
U = unstack(S, vars, ivar)
Spreads the values of vars into new variables named by the categories in ivar.
Description
In long format each measurement is one row, with a column saying what was measured. unstack turns that into one row per group with a column per measurement kind, which is the shape most plotting and statistics functions expect. The remaining variables identify the rows; they must determine a single value for each pair.
stack does the opposite.
Input arguments
S- Table in long format.
vars- Data variable(s) whose values go into the new columns.
ivar- Variable whose distinct values become the names of the new columns.
Output arguments
U- Table with one row per distinct combination of the remaining variables and one new column per category of
ivar.
Example
Long to wide
T = table([1;1;2;2], {'p';'q';'p';'q'}, [10;20;30;40], 'VariableNames', {'id','key','val'});
unstack(T, 'val', 'key')
Output
ans = 2×3 table
id p q
__ __ __
1 10 20
2 30 40