table

Methods

Method Reference: table.rows2vars

table: tblB = rows2vars (tblA)
table: tblB = rows2vars (tblA, Name, Value)

Reorient table by swapping rows into variables.

tblB = rows2vars (tblA) reorients the input table tblA so that its rows become variables in the output table tblB and the variables are swapped into rows and their names are stored into a new variable at the beginning of the output table. If the contents of tblA can be concatenated, then the corresponding variables of tblB are arrays, otherwise they are cell arrays. If the input table tblA contains RowNames, then those names become the variable names of the output table tblB, otherwise the variable names of tblB are generated automatically. rows2vars cannot handle multicolumn variables or nested tables.

tblB = rows2vars (…, Name, Value) further specifies additional parameters for reorienting the table with the following Name-Value paired arguments.

  • 'DataVariables' specifies the variables from input table tblA which will be reoriented. 'DataVariables' can be any of the following types: a character vector specifying a single variable; a cell array of character vectors or a string array specifying a single or multiple variables; a numeric array of integer values specifying a single or multiple variables; a logical vector of the same length as the width of the input table specifying a single or multiple variables.
  • 'VariableNamesSource' specifies a single variable that contains the variable names for the output table. The values of the selected variable must have a data type which can be converted to strings and the number of unique names in the selected variable must match the number of rows of the input table. 'VariableNamesSource' accepts the same data types supported by 'DataVariables' as long as they index a single variable, which, however, must not be specified by the 'DataVariables' Name-Value paired argument.
  • 'VariableNamingRule' must be a character vector specifying the rule for naming variables in the output table tblB. When set to 'modify' (default), the variable names are modified so that they are valid variable identifiers. When set to 'preserve', the original names are preserved.

Source Code: table

Example: 1

rows2vars transposes a table: the original variables become rows and the original rows become variables. The old variable names move into a new leading variable, and the row names (if any) become the new headers.

 Age = [38; 43; 40];
 Height = [71; 69; 64];
 T = table (Age, Height, 'RowNames', {'Li', 'Diaz', 'Brown'})
T =
  3x2 table

             Age    Height    
             ___    ______    

    Li        38        71    
    Diaz      43        69    
    Brown     40        64
 rows2vars (T)
ans =
  2x4 table

    OriginalVariableNames    Li    Diaz    Brown    
    _____________________    __    ____    _____    

    {'Age'              }    38      43       40    
    {'Height'           }    71      69       64