Main Content

rows2vars

Reorient table or timetable so that rows become variables

Description

T2 = rows2vars(T1) reorients the rows of T1, so that they become variables in the output table T2. If rows2vars can concatenate the contents of the rows of T1, then the corresponding variables of T2 are arrays. Otherwise, the variables of T2 are cell arrays. The rows2vars function copies the names of the variables of T1 to a new variable of T2. The output is always a table, though T1 can be either a table or a timetable.

For example, if T1 has two rows and five variables, then T2 has five rows, two variables containing the original data from T1, and a new variable named OriginalVariableNames. In T2, the variable OriginalVariableNames contains the names of the table variables from T1.

If T1 has row names or row times, then those names or times become the variable names of T2. Otherwise, rows2vars generates names Var1,…,VarN as the variable names of T2.

Conceptually, you can think of T2 as being like a transpose of T1. Technically, T2 is not a true transpose of the input table because rows2vars adds a new variable to T2. Also, the variables of T1 might have incompatible data types, in which case the "transposed" rows from T1 become cell arrays in T2.

example

T2 = rows2vars(T1,Name=Value) specifies options using one or more name-value arguments in addition to the input argument in the previous syntax. For example, to specify the source of the variable names of T2, set VariableNamesSource to the variable of T1 that will provide the names of the variables of T2.

example

Examples

collapse all

Create tables, and then reorient their rows to be variables in new tables.

Load arrays of sample patient data from the patients.mat file. Create a table that contains the Age, Height, and Weight arrays from the sample file. Then create a subtable that has only the first five rows of the original table.

load patients
T1 = table(Age,Height,Weight);
T1 = T1(1:5,:)
T1=5×3 table
    Age    Height    Weight
    ___    ______    ______

    38       71       176  
    43       69       163  
    38       64       131  
    40       67       133  
    49       64       119  

To reorient the rows of T1, use the rows2vars function. Because the input table does not have any row names, the variables of the output table have default names, Var1 to VarN for N variables.

T2 = rows2vars(T1)
T2=3×6 table
    OriginalVariableNames    Var1    Var2    Var3    Var4    Var5
    _____________________    ____    ____    ____    ____    ____

         {'Age'   }           38      43      38      40      49 
         {'Height'}           71      69      64      67      64 
         {'Weight'}          176     163     131     133     119 

Create a table with row names using the LastName array from patients.mat. Again create a subtable that has only the first five rows of the original table.

T3 = table(Age,Height,Weight,RowNames=LastName);
T3 = T3(1:5,:)
T3=5×3 table
                Age    Height    Weight
                ___    ______    ______

    Smith       38       71       176  
    Johnson     43       69       163  
    Williams    38       64       131  
    Jones       40       67       133  
    Brown       49       64       119  

Reorient the rows of T3. If the input table has row names, then rows2vars turns the row names into the names of variables of the output table.

T4 = rows2vars(T3)
T4=3×6 table
    OriginalVariableNames    Smith    Johnson    Williams    Jones    Brown
    _____________________    _____    _______    ________    _____    _____

         {'Age'   }            38        43         38         40       49 
         {'Height'}            71        69         64         67       64 
         {'Weight'}           176       163        131        133      119 

Load a timetable from a sample MAT-file and display it.

load bostonTT.mat
Boston
Boston=6×3 timetable
           Time            Temp    WindSpeed    Rain
    ___________________    ____    _________    ____

    2016-06-09 06:03:00    59.5       0.1       0.05
    2016-06-09 12:00:23      63       2.3       0.08
    2016-06-09 18:02:57    61.7       3.1       0.13
    2016-06-10 06:01:47    55.4       5.7       0.15
    2016-06-10 12:06:00    62.3       2.6       0.87
    2016-06-10 18:02:57    58.8       6.2       0.33

Reorient it so that its rows become variables in the output. The rows2vars function turns the row times into names, but modifies them so that they are valid variable names. Also, the output argument returned by rows2vars is always a table, even when the input argument is a timetable.

T = rows2vars(Boston)
Warning: Table variable names that were not valid MATLAB identifiers have been modified. Since table variable names must be unique, any table variable names that happened to match the new identifiers also have been modified.
To use the original row names as new variable names, set 'VariableNamingRule' to 'preserve'.
T=3×7 table
    OriginalVariableNames    x2016_06_0906_03_00    x2016_06_0912_00_23    x2016_06_0918_02_57    x2016_06_1006_01_47    x2016_06_1012_06_00    x2016_06_1018_02_57
    _____________________    ___________________    ___________________    ___________________    ___________________    ___________________    ___________________

        {'Temp'     }               59.5                     63                   61.7                   55.4                   62.3                   58.8        
        {'WindSpeed'}                0.1                    2.3                    3.1                    5.7                    2.6                    6.2        
        {'Rain'     }               0.05                   0.08                   0.13                   0.15                   0.87                   0.33        

To preserve original names from the input when rows2vars sets variable names of the output, use the VariableNamingRule name-value argument.

Load a timetable from a sample MAT-file and display it.

load bostonTT
Boston
Boston=6×3 timetable
           Time            Temp    WindSpeed    Rain
    ___________________    ____    _________    ____

    2016-06-09 06:03:00    59.5       0.1       0.05
    2016-06-09 12:00:23      63       2.3       0.08
    2016-06-09 18:02:57    61.7       3.1       0.13
    2016-06-10 06:01:47    55.4       5.7       0.15
    2016-06-10 12:06:00    62.3       2.6       0.87
    2016-06-10 18:02:57    58.8       6.2       0.33

Reorient the timetable so that its row times become variable names in the output table. Convert the datetime values to strings and preserve the resulting names by setting VariableNamingRule to "preserve".

T = rows2vars(Boston,VariableNamingRule="preserve")
T=3×7 table
    OriginalVariableNames    2016-06-09 06:03:00    2016-06-09 12:00:23    2016-06-09 18:02:57    2016-06-10 06:01:47    2016-06-10 12:06:00    2016-06-10 18:02:57
    _____________________    ___________________    ___________________    ___________________    ___________________    ___________________    ___________________

        {'Temp'     }               59.5                     63                   61.7                   55.4                   62.3                   58.8        
        {'WindSpeed'}                0.1                    2.3                    3.1                    5.7                    2.6                    6.2        
        {'Rain'     }               0.05                   0.08                   0.13                   0.15                   0.87                   0.33        

The variable names in T are not valid MATLAB® identifiers because the dates start with a number. However, you can use dot notation to access such variables using parentheses.

T.("2016-06-09 06:03:00")
ans = 3×1

   59.5000
    0.1000
    0.0500

Load arrays of sample patient data from the patients.mat file. Create a table that contains the Age, Height, Weight, LastName, and Location arrays from the sample file. Then create a subtable that has only the first five rows of the original table.

load patients
T1 = table(Age,Height,Weight,LastName,Location);
T1 = T1(1:5,:)
T1=5×5 table
    Age    Height    Weight      LastName                Location           
    ___    ______    ______    ____________    _____________________________

    38       71       176      {'Smith'   }    {'County General Hospital'  }
    43       69       163      {'Johnson' }    {'VA Hospital'              }
    38       64       131      {'Williams'}    {'St. Mary's Medical Center'}
    40       67       133      {'Jones'   }    {'VA Hospital'              }
    49       64       119      {'Brown'   }    {'County General Hospital'  }

Reorient the rows of T1 to be variables of a new table, T2. Specify that the LastName variable from T1 is the source of the names of the variables of T2. The variables of this output table are cell arrays. They contain values that cannot be concatenated into arrays because they have incompatible data types.

T2 = rows2vars(T1,VariableNamesSource="LastName")
T2=4×6 table
    OriginalVariableNames               Smith                   Johnson                  Williams                    Jones                    Brown           
    _____________________    ___________________________    _______________    _____________________________    _______________    ___________________________

        {'Age'     }         {[                     38]}    {[         43]}    {[                       38]}    {[         40]}    {[                     49]}
        {'Height'  }         {[                     71]}    {[         69]}    {[                       64]}    {[         67]}    {[                     64]}
        {'Weight'  }         {[                    176]}    {[        163]}    {[                      131]}    {[        133]}    {[                    119]}
        {'Location'}         {'County General Hospital'}    {'VA Hospital'}    {'St. Mary's Medical Center'}    {'VA Hospital'}    {'County General Hospital'}

Load arrays of sample patient data from the patients.mat file. Create a table that contains the Age, Height, Weight, Smoker, and Location arrays from the sample file and uses the LastName array as row names. Then create a subtable that has only the first five rows of the original table.

load patients
T1 = table(Age,Height,Weight,Smoker,Location,RowNames=LastName);
T1 = T1(1:5,:)
T1=5×5 table
                Age    Height    Weight    Smoker              Location           
                ___    ______    ______    ______    _____________________________

    Smith       38       71       176      true      {'County General Hospital'  }
    Johnson     43       69       163      false     {'VA Hospital'              }
    Williams    38       64       131      false     {'St. Mary's Medical Center'}
    Jones       40       67       133      false     {'VA Hospital'              }
    Brown       49       64       119      false     {'County General Hospital'  }

Reorient specified variables from T1 and discard the rest.

T2 = rows2vars(T1,DataVariables=["Age" "Height" "Weight"])
T2=3×6 table
    OriginalVariableNames    Smith    Johnson    Williams    Jones    Brown
    _____________________    _____    _______    ________    _____    _____

         {'Age'   }            38        43         38         40       49 
         {'Height'}            71        69         64         67       64 
         {'Weight'}           176       163        131        133      119 

You also can specify data variables by position in the input table. To specify positions, use a numeric array.

T3 = rows2vars(T1,DataVariables=1:3)
T3=3×6 table
    OriginalVariableNames    Smith    Johnson    Williams    Jones    Brown
    _____________________    _____    _______    ________    _____    _____

         {'Age'   }            38        43         38         40       49 
         {'Height'}            71        69         64         67       64 
         {'Weight'}           176       163        131        133      119 

Input Arguments

collapse all

Input table, specified as a table or timetable.

Name-Value Arguments

collapse all

Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Example: VariableNamingRule="preserve" preserves original names taken from T1, without modifying them to be valid MATLAB® identifiers.

Variable in T1 that contains variable names, specified as a string scalar, character vector, positive integer, or logical vector. The rows2vars function interprets the value of VariableNamesSource as shown in the table.

Value of VariableNamesSource

Meaning

String scalar or character vector

Name of a variable in the input table T1.

Integer n

The nth variable in T1.

Logical vector, whose length equals the number of variables in T1

The nth element is 1 (true), corresponding to the nth variable of T1. All other elements are 0 (false).

While the value of VariableNamesSource must be a name, number, or logical array that specifies a table variable, the table variable itself can have any data type, with these limitations.

  • The values contained in the specified table variable must have a data type that allows the values to be converted to strings. For example, the value of VariableNamesSource can be the name of a table variable that contains a datetime array, because datetime values can be converted to strings.

  • The number of names taken from the specified table variable must match the number of rows of the input table.

The value of VariableNamesSource cannot specify a variable that is also specified by the DataVariables name-value argument.

Selected variables from T1, specified as a string array, character vector, cell array of character vectors, pattern scalar, positive integer, vector of positive integers, or logical vector. The rows2vars function selects the variables specified by the value of DataVariables and reorients only those variables to become the rows of T2. The remaining variables of T1 are discarded.

The value of DataVariables cannot specify a variable that is also specified by the VariableNamesSource name-value argument.

Rule for naming variables in T2, specified as "modify" or "preserve".

The values of VariableNamingRule specify the following rules for naming variable in the output table or timetable.

Value of VariableNamingRule

Rule

"modify" (default)

Modify names taken from the input table or timetable so that the corresponding variable names in the output are also valid MATLAB identifiers.

"preserve"

Preserve original names taken from the input table or timetable. The corresponding variable names in the output can have any Unicode® characters, including spaces and non-ASCII characters.

Note: In some cases, rows2vars must modify original names even when "preserve" is the rule. Such cases include:

  • Duplicate names

  • Names that conflict with table dimension names

  • Names that conflict with a reserved name.

  • Names whose lengths exceed the value of namelengthmax.

Output Arguments

collapse all

Reoriented output table.

If the input is a timetable, then the output is a table whose variable names represent the row times of the input timetable.

Extended Capabilities

expand all

Thread-Based Environment
Run code in the background using MATLAB® backgroundPool or accelerate code with Parallel Computing Toolbox™ ThreadPool.

Version History

Introduced in R2018a