stack
Stack data from input table or timetable into one variable in output table or timetable
Description
converts the input table or timetable, S
= stack(U
,vars
)U
, into the stacked table
or timetable, S
. The function stacks values from multiple
variables in U
into one variable in S
. The
input argument vars
specifies which variables from
U
to stack, either by their names or their positions in
U
.
If U
has n
rows and vars
specifies m
variables in U
, then
S
has m*n
rows. The
stack
function interleaves values from the specified
variables in U
to create one variable in S
.
For example, if U
has 10 rows and you stack three of the
variables from U
, then S
has 30 rows. In
general, S
contains fewer variables, but more rows, than
U
.
The output table or timetable, S
, contains a new
categorical
variable to indicate which variable in
U
the stacked data in each row of S
comes
from. The stack
function replicates data from the variables in
U
that are not stacked.
If
U
is a table, then you cannot stack row names.If
U
is a timetable, then you cannot stack row times.
converts the table, S
= stack(U
,vars
,Name,Value
)U
, with additional options specified by one
or more Name,Value
pair arguments.
For example, you can specify variable names for the new and stacked variables in
S
.
Examples
Input Arguments
Output Arguments
Tips
Create Multiple Stacked Variables in Output
It is possible to stack values from the input into multiple variables in the output. To create multiple stacked variables in
S
, use a cell array to specify multiple groups of variables fromU
. You can use a cell array to contain multiple values forvars
, and a cell array of character vectors or string array to contain multiple values for the'NewDataVariableName'
name-value pair argument. All groups must contain the same number of variables.For example, if
U
contains four variables, then you can create two stacked variables inS
, instead of only one, by using this syntax.vars = {[1 2],[3 4]}; S = stack(U,vars)
As a result,
S
has a stacked variable with values from the first and second variables ofU
, and another stacked variable with values from the third and fourth variables ofU
.To specify new variable names in
S
, use'NewDataVariableName'
. The number of names you specify must equal the number of groups specified invars
.vars = {[1 2],[3 4]}; S = stack(U,vars,'NewDataVariableName',{'Vars1And2','Vars3And4'})
When you specify
vars
as a cell array of character vectors, thenS
contains one stacked variable. To specify multiple stacked variables while using variable names, use a cell array of cell arrays of character vectors, or a cell array of string arrays.For example, this syntax creates one stacked variable in
S
whenU
is a table with variables namedVar1
,Var2
,Var3
, andVar4
.S = stack(U,{'Var1','Var2','Var3','Var4'})
This syntax creates two stacked variables in
S
.S = stack(U,{{'Var1','Var2'} {'Var3','Var4'}})
Extended Capabilities
Version History
Introduced in R2013b
See Also
unstack
| join
| Stack Table
Variables | Unstack Table
Variables | Join Tables