Main Content

mergevars

Combine table or timetable variables into multicolumn variable

Description

example

T2 = mergevars(T1,vars) combines the table variables specified by vars to create one multicolumn variable in T2 (see diagram). All other variables from T1 are unaltered. You can specify variables by name, by position, or using logical indices.

By default, the name of the merged variable in T2 takes the form VarN, where N is the position of the merged variable. For example, if the merged variable is the third variable in T2, then its name is Var3.

To split multicolumn variables, use the splitvars function.

example

T2 = mergevars(T1,vars,'NewVariableName',newName) specifies a name for the multicolumn variable.

example

T2 = mergevars(___,'MergeAsTable',true) merges the specified variables into a table, instead of an array. The new table is itself a variable of the output table T2. Use this syntax to combine variables that cannot be concatenated into an array. You can use this syntax with any of the input arguments from the previous syntaxes.

Examples

collapse all

Create a table from workspace variables.

A = [1:3]';
B = [5 11 12]';
C = [3.14 2.72 1.37]';
D = {'a';'b';'c'};
T1 = table(A,B,C,D)
T1=3×4 table
    A    B      C        D  
    _    __    ____    _____

    1     5    3.14    {'a'}
    2    11    2.72    {'b'}
    3    12    1.37    {'c'}

Merge the second and third variables. The new variable has two columns.

T2 = mergevars(T1,[2 3])
T2=3×3 table
    A       Var2         D  
    _    __________    _____

    1     5    3.14    {'a'}
    2    11    2.72    {'b'}
    3    12    1.37    {'c'}

Create a table using arrays of data from the patients.mat file. Display the first three rows of the table.

load patients
T1 = table(LastName,Gender,Age,Height,Weight,Systolic,Diastolic);
head(T1,3)
ans=3×7 table
      LastName        Gender      Age    Height    Weight    Systolic    Diastolic
    ____________    __________    ___    ______    ______    ________    _________

    {'Smith'   }    {'Male'  }    38       71       176        124          93    
    {'Johnson' }    {'Male'  }    43       69       163        109          77    
    {'Williams'}    {'Female'}    38       64       131        125          83    

Merge the variables Systolic and Diastolic into one variable with two columns. Name it BloodPressure.

T2 = mergevars(T1,{'Systolic','Diastolic'},'NewVariableName','BloodPressure');
head(T2,3)
ans=3×6 table
      LastName        Gender      Age    Height    Weight    BloodPressure
    ____________    __________    ___    ______    ______    _____________

    {'Smith'   }    {'Male'  }    38       71       176       124     93  
    {'Johnson' }    {'Male'  }    43       69       163       109     77  
    {'Williams'}    {'Female'}    38       64       131       125     83  

Read in a table from a spreadsheet. Display the first three rows.

T1 = readtable('outages.csv');
head(T1,3)
ans =

  3x6 table

       Region           OutageTime        Loss     Customers     RestorationTime          Cause      
    _____________    ________________    ______    __________    ________________    ________________

    {'SouthWest'}    2002-02-01 12:18    458.98    1.8202e+06    2002-02-07 16:50    {'winter storm'}
    {'SouthEast'}    2003-01-23 00:49    530.14    2.1204e+05                 NaT    {'winter storm'}
    {'SouthEast'}    2003-02-07 21:15     289.4    1.4294e+05    2003-02-17 08:14    {'winter storm'}

Merge Cause, Loss, and RestorationTime. Because these variables have different types, merge them into a table within a table.

T2 = mergevars(T1,{'Cause','Loss','RestorationTime'},...
               'NewVariableName','LossData','MergeAsTable',true);
head(T2,3)
ans =

  3x4 table

       Region           OutageTime       Customers                        LossData                   
                                                            Cause           Loss     RestorationTime 
    _____________    ________________    __________    ______________________________________________

    {'SouthWest'}    2002-02-01 12:18    1.8202e+06    {'winter storm'}    458.98    2002-02-07 16:50
    {'SouthEast'}    2003-01-23 00:49    2.1204e+05    {'winter storm'}    530.14                 NaT
    {'SouthEast'}    2003-02-07 21:15    1.4294e+05    {'winter storm'}     289.4    2003-02-17 08:14

Input Arguments

collapse all

Input table, specified as a table or timetable.

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

Name of the merged variable, specified as a character vector or string scalar.

Extended Capabilities

Introduced in R2018a