Documentation Center

  • Trial Software
  • Product Updates

stack

Stack data from multiple variables into single variable

Syntax

Description

example

T = stack(W,vars) converts the wide table, W, into an equivalent table, T, that is in tall format. stack stacks up multiple variables from W, specified by vars, into a single variable in T. In general, T contains fewer variables, but more rows, than W.

The output table, T, contains a new categorical variable to indicate which variable in W the stacked data in each row came from. stack replicates data from the variables in W that are not stacked.

example

T = stack(W,vars,Name,Value) converts the table, W, to tall format 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 W.

example

[T,iw] = stack(___) also returns an index vector, iw, indicating the correspondence between rows in T and rows in W. You can use any of the previous input arguments.

Examples

expand all

Stack Three Variables into One

Create a table containing test scores from three separate tests.

Test1 = [93;57;87;89];
Test2 = [89;77;92;86];
Test3 = [95;62;89;91];

W = table(Test1,Test2,Test3)
W = 

    Test1    Test2    Test3
    _____    _____    _____

    93       89       95   
    57       77       62   
    87       92       89   
    89       86       91   

The table contains four rows and three variables.

Stack the test scores into a single variable.

T = stack(W,1:3)
T = 

    Test1_Test2_Test3_Indicator    Test1_Test2_Test3
    ___________________________    _________________

    Test1                          93               
    Test2                          89               
    Test3                          95               
    Test1                          57               
    Test2                          77               
    Test3                          62               
    Test1                          87               
    Test2                          92               
    Test3                          89               
    Test1                          89               
    Test2                          86               
    Test3                          91               

T contains twelve rows and two variables.

The categorical variable, Test1_Test2_Test3_Indicator, identifies which test corresponds to the score in the stacked data variable, Test1_Test2_Test3.

Stack Variables and Specify Variable Names

Create a table indicating the amount of snowfall at three locations from five separate storms.

Storm = [1;2;3;4;5];
Date = {'12/25/11';'1/2/12';'1/23/12';'2/7/12';'2/15/12'};
Natick = [20;5;13;0;17];
Boston = [18;9;21;5;12];
Worcester = [26;10;16;3;15];

W = table(Storm,Date,Natick,Boston,Worcester)
W = 

    Storm       Date       Natick    Boston    Worcester
    _____    __________    ______    ______    _________

    1        '12/25/11'    20        18        26       
    2        '1/2/12'       5         9        10       
    3        '1/23/12'     13        21        16       
    4        '2/7/12'       0         5         3       
    5        '2/15/12'     17        12        15       

The variables Storm and Date contain data that is constant at each location.

Stack the variables Natick, Boston, and Worcester into a single variable. Name the variable containing the stacked data, Snowfall, and name the new indicator variable, Town.

T = stack(W,{'Natick','Boston','Worcester'},...
    'NewDataVariableName','Snowfall',...
    'IndexVariableName','Town')
T = 

    Storm       Date         Town       Snowfall
    _____    __________    _________    ________

    1        '12/25/11'    Natick       20      
    1        '12/25/11'    Boston       18      
    1        '12/25/11'    Worcester    26      
    2        '1/2/12'      Natick        5      
    2        '1/2/12'      Boston        9      
    2        '1/2/12'      Worcester    10      
    3        '1/23/12'     Natick       13      
    3        '1/23/12'     Boston       21      
    3        '1/23/12'     Worcester    16      
    4        '2/7/12'      Natick        0      
    4        '2/7/12'      Boston        5      
    4        '2/7/12'      Worcester     3      
    5        '2/15/12'     Natick       17      
    5        '2/15/12'     Boston       12      
    5        '2/15/12'     Worcester    15      

T contains three rows for each storm, and stack repeats the data in the constant variables, Storm and Date, accordingly.

The categorical variable, Town, identifies which variable in W contains the corresponding Snowfall data.

Stack Variables and Output an Index Vector

Create a table containing estimated influenza rates along the east coast of the United States. Create a different variable for the Northeast, Mid Atlantic, and South Atlantic. Data Source: Google Flu Trends (http://www.google.org/flutrends).

Month = {'October';'November';'December';...
    'January';'February';'March'};
Year = [2005*ones(3,1); 2006*ones(3,1)];
NE = [1.1902; 1.3610; 1.5003; 1.7772; 2.1350; 2.2345];
MidAtl = [1.1865; 1.4120; 1.6043; 1.8830; 2.1227; 1.9920];
SAtl = [1.2730; 1.5820; 1.8625; 1.9540; 2.4803; 2.0203];

fluW = table(Month,Year,NE,MidAtl,SAtl)
fluW = 

      Month       Year      NE      MidAtl     SAtl 
    __________    ____    ______    ______    ______

    'October'     2005    1.1902    1.1865     1.273
    'November'    2005     1.361     1.412     1.582
    'December'    2005    1.5003    1.6043    1.8625
    'January'     2006    1.7772     1.883     1.954
    'February'    2006     2.135    2.1227    2.4803
    'March'       2006    2.2345     1.992    2.0203

The variables Month and Year contain data that is constant across the row.

Stack the variables NE, MidAtl, and SAtl into a single variable called FluRate. Name the new indicator variable Region and output an index vector, ifluW, to indicate the correspondence between rows in the input wide table, fluW, and the output tall table, fluT.

[fluT,ifluW] = stack(fluW,3:5,...
    'NewDataVariableName','FluRate',...
    'IndexVariableName','Region')
fluT = 

      Month       Year    Region    FluRate
    __________    ____    ______    _______

    'October'     2005    NE        1.1902 
    'October'     2005    MidAtl    1.1865 
    'October'     2005    SAtl       1.273 
    'November'    2005    NE         1.361 
    'November'    2005    MidAtl     1.412 
    'November'    2005    SAtl       1.582 
    'December'    2005    NE        1.5003 
    'December'    2005    MidAtl    1.6043 
    'December'    2005    SAtl      1.8625 
    'January'     2006    NE        1.7772 
    'January'     2006    MidAtl     1.883 
    'January'     2006    SAtl       1.954 
    'February'    2006    NE         2.135 
    'February'    2006    MidAtl    2.1227 
    'February'    2006    SAtl      2.4803 
    'March'       2006    NE        2.2345 
    'March'       2006    MidAtl     1.992 
    'March'       2006    SAtl      2.0203 


ifluW =

     1
     1
     1
     2
     2
     2
     3
     3
     3
     4
     4
     4
     5
     5
     5
     6
     6
     6

ifluW(5) is 2. The fifth row in the output table, fluT, contains data from the second row in the input table fluW.

Input Arguments

expand all

W — Wide tabletable

Wide table, specified as a table.

vars — Variables in W to stackpositive integer | vector of positive integers | variable name | cell array of variable names | logical vector

Variables in W to stack, specified as a positive integer, vector of positive integers, variable name, cell array of variable names, or logical vector.

Name-Value Pair Arguments

Specify optional comma-separated pairs of Name,Value arguments. Name is the argument name and Value is the corresponding value. Name must appear inside single quotes (' '). You can specify several name and value pair arguments in any order as Name1,Value1,...,NameN,ValueN.

Example: 'NewDataVariableName','StackedData' names the new data variable StackedData.

'ConstantVariables' — Variables other than vars to include in the outputpositive integer | vector of positive integers | variable name | cell array of variable names | logical vector

Variables other than vars to include in the output, specified as the comma-separated pair consisting of 'ConstantVariables' and a positive integer, vector of positive integers, variable name, cell array of variable names, or logical vector. stack replicates the data from the constant variables for each stacked entry from a row.

The default is all the variables in W not specified by vars. You can specify the 'ConstantVariables' name-value pair argument to exclude variables not specified by vars or 'ConstantVariables' from the output table, T.

'NewDataVariableName' — Name for the new data variable in Tstring

Name for the new data variable in T, specified as the comma-separated pair consisting of 'NewDataVariableName' and a string. The default is a concatenation of the names of the variables from W that are stacked up.

'IndexVariableName' — Name for the new indicator variable in Tstring

Name for the new indicator variable in T, specified as the comma-separated pair consisting of 'IndexVariableName' and a string. The default is a name based on NewDataVariableName.

Output Arguments

expand all

T — Tall tabletable

Tall table, returned as a table. T contains a stacked data variable, a categorical indicator variable, and any constant variables.

You can store additional metadata such as descriptions, variable units, variable names, and row names in the table. For more information, see Table Properties.

stack assigns the variable units and variable description property values from the first variable listed in vars to the corresponding T.Properties.VariableUnits and T.Properties.VariableDescrisciptions values for the new data variable.

iw — Index to Wcolumn vector

Index to W, returned as a column vector. The index vector, iw, identifies the row in the input table, W, containing the corresponding data. stack creates the jth row in the output table, T, using W(iw(j),vars).

More About

expand all

Tips

  • You can specify more than one group of data variables in W, and each group becomes a stacked data variable in T. Use a cell array to contain multiple values for vars, and a cell array of strings to contain multiple values for the 'NewDataVariableName' name-value pair argument. All groups must contain the same number of variables.

See Also

|

Was this topic helpful?