Documentation

This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

unstack

Unstack data from single variable into multiple variables

Syntax

U = unstack(S,vars,ivar)
U = unstack(S,vars,ivar,Name,Value)
[U,is] = unstack(___)

Description

example

U = unstack(S,vars,ivar) converts the table or timetable, S, to an equivalent table or timetable, U, that is unstacked. vars specifies variables in S, each of which is unstacked into multiple variables in U. In general, U contains more variables, but fewer rows, than S.

The ivar input argument specifies the variable in S that unstack uses as an indicator variable. The values in ivar determine which variables in U contain elements taken from vars after unstacking.

The unstack function treats the remaining variables differently in tables and timetables.

  • If S is a table, then unstack treats the remaining variables as grouping variables. Each unique combination of values in the grouping variables identifies a group of rows in S that is unstacked into a single row of U.

  • If S is a timetable, then unstack discards the remaining variables. However, unstack treats the vector of row times as a grouping variable.

You cannot unstack the row names of a table, or the row times of a timetable, or specify either as the indicator variable. You can specify row names or row times as constant variables with the 'ConstantVariables' argument.

U = unstack(S,vars,ivar,Name,Value) converts the table or timetable S with additional options specified by one or more Name,Value pair arguments.

For example, you can specify how unstack converts variables from S to variables in U.

example

[U,is] = unstack(___) also returns an index vector, is, indicating the correspondence between rows in U and rows in S. You can use any of the previous input arguments.

Examples

collapse all

Create a table indicating the amount of snowfall in various towns for various storms.

Storm = [3;3;1;3;1;1;4;2;4;2;4;2];
Town = {'T1';'T3';'T1';'T2';'T2';'T3';...
    'T2';'T1';'T3';'T3';'T1';'T2'};
Snowfall = [0;3;5;5;9;10;12;13;15;16;17;21];

S = table(Storm,Town,Snowfall)
S=12×3 table
    Storm    Town    Snowfall
    _____    ____    ________

      3      'T1'        0   
      3      'T3'        3   
      1      'T1'        5   
      3      'T2'        5   
      1      'T2'        9   
      1      'T3'       10   
      4      'T2'       12   
      2      'T1'       13   
      4      'T3'       15   
      2      'T3'       16   
      4      'T1'       17   
      2      'T2'       21   

S contains three snowfall entries for each storm, one for each town. S is in stacked format.

Separate the variable Snowfall into three variables, one for each town specified in the variable, Town. The output table, U, is in unstacked format.

U = unstack(S,'Snowfall','Town')
U=4×4 table
    Storm    T1    T2    T3
    _____    __    __    __

      3       0     5     3
      1       5     9    10
      4      17    12    15
      2      13    21    16

Each row in U contains data from rows in S that have the same value in the grouping variable, Storm. The order of the unique values in Storm determines the order of the data in U.

Unstack data and apply an aggregation function to multiple rows in the same group that have the same values in the indicator variable.

Create a table containing data on the price of two stocks over 2 days.

Date = [repmat({'4/12/2008'},6,1);...
    repmat({'4/13/2008'},5,1)];
Stock = {'Stock1';'Stock2';'Stock1';'Stock2';...
    'Stock2';'Stock2';'Stock1';'Stock2';...
    'Stock2';'Stock1';'Stock2'};
Price = [60.35;27.68;64.19;25.47;28.11;27.98;...
    63.85;27.55;26.43;65.73;25.94];

S = table(Date,Stock,Price)
S=11×3 table
       Date         Stock      Price
    ___________    ________    _____

    '4/12/2008'    'Stock1'    60.35
    '4/12/2008'    'Stock2'    27.68
    '4/12/2008'    'Stock1'    64.19
    '4/12/2008'    'Stock2'    25.47
    '4/12/2008'    'Stock2'    28.11
    '4/12/2008'    'Stock2'    27.98
    '4/13/2008'    'Stock1'    63.85
    '4/13/2008'    'Stock2'    27.55
    '4/13/2008'    'Stock2'    26.43
    '4/13/2008'    'Stock1'    65.73
    '4/13/2008'    'Stock2'    25.94

S contains two prices for STOCK1 during the first day and four prices for STOCK2 during the first day.

Create a table containing separate variables for each stock and one row for each day. Use Date as the grouping variable and apply the aggregation function, @mean, to the numeric values from the variable, Price, for each group.

[U,is] = unstack(S,'Price','Stock',...
    'AggregationFunction',@mean)
U=2×3 table
       Date        Stock1    Stock2
    ___________    ______    ______

    '4/12/2008'    62.27     27.31 
    '4/13/2008'    64.79     26.64 

is = 2×1

     1
     7

U contains the average price for each stock grouped by date.

is identifies the index of the first value for each group of rows in S. The first value for the group '4/13/2008' is in the seventh row of S.

Input Arguments

collapse all

Input table, specified as a table or a timetable. S must contain data variables to unstack, vars, and an indicator variable, ivar. The remaining variables in S can be treated as either grouping variables or constant variables.

Variables in S to unstack, specified as a positive integer, vector of positive integers, character vector, cell array of character vectors, or logical vector.

Indicator variable in S, specified as a positive integer or a character vector. The values in the variable specified by ivar indicate which variables in U contain elements taken from the variables specified by vars.

The variable specified by ivar can be a numeric vector, logical vector, character array, cell array of character vectors, or categorical 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: 'AggregationFunction',@mean applies the aggregation function @mean to the values in vars.

collapse all

Grouping variables in S that define groups of rows, specified as the comma-separated pair consisting of 'GroupingVariables' and a positive integer, vector of positive integers, character vector, cell array of character vectors, or logical vector. Each group of rows in S becomes one row in U.

S can have row labels along its first dimension. If S is a table, then it can have row names as the labels. If S is a timetable, then it must have row times as the labels. unstack can treat row labels as grouping variables.

  • If you do not specify 'GroupingVariables', and S is a timetable, then unstack treats the row times as a grouping variable.

  • If you specify 'GroupingVariables', and S has row names or row times, then unstack does not treat them as grouping variables, unless you include them in the value of 'GroupingVariables'.

Variables constant within a group, specified as the comma-separated pair consisting of 'ConstantVariables' and a positive integer, vector of positive integers, character vector, cell array of character vectors, or logical vector.

The values for these variables in U are taken from the first row in each group in S.

You can include the row names or row times of S when you specify the value of 'ConstantVariables'.

Names for the new data variables in U, specified as the comma-separated pair consisting of 'NewDataVariableNames' and a cell array of character vectors.

If you do not specify 'NewDataVariableNames', then unstack creates names for the new data variables in U based on values in the indicator variable specified by ivar.

Aggregation function from values in vars to a single value, specified as the comma-separated pair consisting of 'AggregationFunction' and a function handle. unstack applies this function to rows from the same group that have the same value in ivar. The function must aggregate the data values into a single value.

For a numeric data variable, the default is @sum. For nonnumeric variables, there is no default function, and you must specify the 'AggregationFunction' name-value pair argument if multiple rows in the same group have the same value in ivar.

Output Arguments

collapse all

Output table, returned as a table or a timetable. U contains the unstacked data variables, the grouping variables, and the first value of each group from any constant variables.

The order of the data in U is based on the order of the unique values in the grouping variables.

You can store additional metadata such as descriptions, variable units, variable names, and row names in U. For more information, see the Properties sections of table or timetable.

Index to S, returned as a column vector. For each row in U, the index vector, is, identifies the index of the first value in the corresponding group of rows in S.

More About

collapse all

Grouping Variables

Grouping variables are utility variables used to group, or categorize, data. Grouping variables are useful for summarizing or visualizing data by group. You can define groups in your table by specifying one or more grouping variables.

A grouping variable can be any of the following:

  • Categorical vector

  • Cell array of character vectors

  • Character array

  • Numeric vector, typically containing positive integers

  • Logical vector

Rows that have the same grouping variable value belong to the same group. If you use multiple grouping variables, rows that have the same combination of grouping variable values belong to the same group.

Tips

  • You can specify more than one data variable in S, and each variable becomes a set of unstacked data variables in U. Use a vector of positive integers, a cell array containing multiple variable names, or a logical vector to specify vars. The one indicator variable, specified by the input argument, ivar, applies to all data variables specifies by vars.

See Also

|

Introduced in R2013b

Was this topic helpful?