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.

rows2vars

Reorient table or timetable so that rows become variables

Syntax

T2 = rows2vars(T1)
T2 = rows2vars(T1,'VariableNamesSource',varnames)
T2 = rows2vars(___,'DataVariables',datavars)

Description

example

T2 = rows2vars(T1) reorients the rows of T1, so that they become variables in the output table T2, as shown in the diagram. 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. rows2vars always returns a table, though T1 can be either a table or a timetable.

The rows2vars function copies the names of the variables of T1 to a new variable of T2. 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.

example

T2 = rows2vars(T1,'VariableNamesSource',varnames) specifies a variable in T1 as the source of the names given to the variables of T2. You can specify varnames as a variable name or a numeric or logical index.

example

T2 = rows2vars(___,'DataVariables',datavars) selects the variables of T1 specified by datavars and reorients only those variables to become the rows of T2. The remaining variables of T1 are discarded. datavars can be one or more variable names, an array of numeric or logical indices, or a subscripting object (for example, the output of the vartype function). You can use this syntax with the input arguments of either of the previous syntaxes.

If you specify a variable using the 'VariableNamesSource' name-value pair argument from the previous syntax, then you cannot specify that variable in datavars.

Examples

collapse all

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

Load arrays of data from the patients.mat file. Create a table that contains the LastName, Gender, Age, Height, and Weight variables.

load patients
T1 = table(LastName,Gender,Age,Height,Weight);
head(T1,3)
ans=3×5 table
     LastName      Gender     Age    Height    Weight
    __________    ________    ___    ______    ______

    'Smith'       'Male'      38       71       176  
    'Johnson'     'Male'      43       69       163  
    'Williams'    'Female'    38       64       131  

Reorient the rows of T1 to be the variables of the output table.

T2 = rows2vars(T1);

Display the first four variables of T2. The first variable of T2 contains the names of the variables of T1. The remaining variables of T2 correspond to rows of T1. Since T1 did not have any row labels, the variables of T2 have default names, Var1 to VarN for N variables.

T2(:,1:4)
ans=5×4 table
    OriginalVariableNames     Var1        Var2          Var3   
    _____________________    _______    _________    __________

         'LastName'          'Smith'    'Johnson'    'Williams'
         'Gender'            'Male'     'Male'       'Female'  
         'Age'               [   38]    [     43]    [      38]
         'Height'            [   71]    [     69]    [      64]
         'Weight'            [  176]    [    163]    [     131]

Create a table with row names. If a table has row names, then rows2vars turns the row names into the names of variables.

T3 = table(Gender,Age,Height,Weight,'RowNames',LastName);
head(T3,3)
ans=3×4 table
                 Gender     Age    Height    Weight
                ________    ___    ______    ______

    Smith       'Male'      38       71       176  
    Johnson     'Male'      43       69       163  
    Williams    'Female'    38       64       131  

Reorient the rows of T3.

T4 = rows2vars(T3);
T4(:,1:4)
ans=4×4 table
    OriginalVariableNames    Smith     Johnson    Williams
    _____________________    ______    _______    ________

          'Gender'           'Male'    'Male'     'Female'
          'Age'              [  38]    [  43]     [    38]
          'Height'           [  71]    [  69]     [    64]
          'Weight'           [ 176]    [ 163]     [   131]

Load a timetable 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 it so that its rows become variables in the output. The rows2vars function turns the row times into names, but must modify 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: Variable names were modified to make them valid MATLAB identifiers.
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        

Read data from a spreadsheet into a table. Display the first three rows.

T1 = readtable('patients.xls');
head(T1,3)
ans=3×10 table
     LastName      Gender     Age             Location              Height    Weight    Smoker    Systolic    Diastolic    SelfAssessedHealthStatus
    __________    ________    ___    ___________________________    ______    ______    ______    ________    _________    ________________________

    'Smith'       'Male'      38     'County General Hospital'        71       176      true        124          93              'Excellent'       
    'Johnson'     'Male'      43     'VA Hospital'                    69       163      false       109          77              'Fair'            
    'Williams'    'Female'    38     'St. Mary's Medical Center'      64       131      false       125          83              'Good'            

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.

T2 = rows2vars(T1,'VariableNamesSource','LastName');

Display the first four variables of T2. The first variable of T2 contains the names of the variables of T1. The remaining variables of T2 correspond to rows of T1.

T2(:,1:4)
ans=9×4 table
      OriginalVariableNames                 Smith                 Johnson                Williams          
    __________________________    _________________________    _____________    ___________________________

    'Gender'                      'Male'                       'Male'           'Female'                   
    'Age'                         [                     38]    [         43]    [                       38]
    'Location'                    'County General Hospital'    'VA Hospital'    'St. Mary's Medical Center'
    'Height'                      [                     71]    [         69]    [                       64]
    'Weight'                      [                    176]    [        163]    [                      131]
    'Smoker'                      [                      1]    [          0]    [                        0]
    'Systolic'                    [                    124]    [        109]    [                      125]
    'Diastolic'                   [                     93]    [         77]    [                       83]
    'SelfAssessedHealthStatus'    'Excellent'                  'Fair'           'Good'                     

Display the data in T2.Smith. In this example, every variable of T2 is a 9-by-1 cell array, because the values in the rows of T1 cannot be concatenated into arrays.

T2.Smith
ans = 9x1 cell array
    {'Male'                   }
    {[                     38]}
    {'County General Hospital'}
    {[                     71]}
    {[                    176]}
    {[                      1]}
    {[                    124]}
    {[                     93]}
    {'Excellent'              }

Read data from a spreadsheet into a table. Use the first column of the spreadsheet as the row names of the table. Display the first three rows.

T1 = readtable('patients.xls','ReadRowNames',true);
head(T1,3)
ans=3×9 table
                 Gender     Age             Location              Height    Weight    Smoker    Systolic    Diastolic    SelfAssessedHealthStatus
                ________    ___    ___________________________    ______    ______    ______    ________    _________    ________________________

    Smith       'Male'      38     'County General Hospital'        71       176      true        124          93              'Excellent'       
    Johnson     'Male'      43     'VA Hospital'                    69       163      false       109          77              'Fair'            
    Williams    'Female'    38     'St. Mary's Medical Center'      64       131      false       125          83              'Good'            

Reorient specified variables from T1 and discard the rest. To specify data variables by name, use a cell array of character vectors.

T2 = rows2vars(T1,'DataVariables',{'Gender','Age','Height','Weight'});
T2(:,1:4)
ans=4×4 table
    OriginalVariableNames    Smith     Johnson    Williams
    _____________________    ______    _______    ________

          'Gender'           'Male'    'Male'     'Female'
          'Age'              [  38]    [  43]     [    38]
          'Height'           [  71]    [  69]     [    64]
          'Weight'           [ 176]    [ 163]     [   131]

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

T3 = rows2vars(T1,'DataVariables',[1 2 6:9]);
T3(:,1:4)
ans=6×4 table
      OriginalVariableNames          Smith       Johnson    Williams
    __________________________    ___________    _______    ________

    'Gender'                      'Male'         'Male'     'Female'
    'Age'                         [       38]    [  43]     [    38]
    'Smoker'                      [        1]    [   0]     [     0]
    'Systolic'                    [      124]    [ 109]     [   125]
    'Diastolic'                   [       93]    [  77]     [    83]
    'SelfAssessedHealthStatus'    'Excellent'    'Fair'     'Good'  

Input Arguments

collapse all

Input table, specified as a table or timetable.

Source of names for the output table variables, specified as a character vector, string scalar, integer, or logical array.

  • If varnames is a character vector or string scalar, then it is the name of a variable in the input table T1.

  • If varnames is the integer n, then it indicates the nth variable in T1.

  • If varnames is a logical array, then it indicates the nth variable by specifying its nth element as 1 (true). All other elements must be 0 (false).

While varnames must be a name, number, or logical array that specifies a table variable, the 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, varnames 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.

Variables in the input table, specified as a character vector, cell array of character vectors, string array, numeric array, logical array, or subscripting object.

Introduced in R2018a