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. All other variables from T1 are unaltered. You can specify variables by name, by position, or using logical indices.

For example, if T1 has variables named var3 and var5, then you can combine them into a variable that has two columns by using T2 = mergevars(T1,["var3","var5"]).

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,Name,Value) specifies options using one or more name-value arguments in addition to the input arguments in the previous syntax. For example, to specify a name for the merged variable, set NewVariableName to the name that you specify.

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.

load patients
T1 = table(Age,Height,Weight,Systolic,Diastolic)
T1=100×5 table
    Age    Height    Weight    Systolic    Diastolic
    ___    ______    ______    ________    _________

    38       71       176        124          93    
    43       69       163        109          77    
    38       64       131        125          83    
    40       67       133        117          75    
    49       64       119        122          80    
    46       68       142        121          70    
    33       64       142        130          88    
    40       68       180        115          82    
    28       68       183        115          78    
    31       66       132        118          86    
    45       68       128        114          77    
    42       66       137        115          68    
    25       71       174        127          74    
    39       72       202        130          95    
    36       65       129        114          79    
    48       71       181        130          92    
      ⋮

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

T2 = mergevars(T1,["Systolic","Diastolic"], ...
               "NewVariableName","BloodPressure")
T2=100×4 table
    Age    Height    Weight    BloodPressure
    ___    ______    ______    _____________

    38       71       176       124     93  
    43       69       163       109     77  
    38       64       131       125     83  
    40       67       133       117     75  
    49       64       119       122     80  
    46       68       142       121     70  
    33       64       142       130     88  
    40       68       180       115     82  
    28       68       183       115     78  
    31       66       132       118     86  
    45       68       128       114     77  
    42       66       137       115     68  
    25       71       174       127     74  
    39       72       202       130     95  
    36       65       129       114     79  
    48       71       181       130     92  
      ⋮

Read a table from a spreadsheet. To specify the data types of the columns that you read from the spreadsheet, use an import options object. Display the first eight rows of the output table.

opts = detectImportOptions("outages.csv");
opts.VariableTypes = ["categorical","datetime","double", ...
                      "double","datetime","categorical"];
T1 = readtable("outages.csv",opts)
T1=1468×6 table
     Region           OutageTime          Loss     Customers       RestorationTime            Cause     
    _________    ____________________    ______    __________    ____________________    _______________

    SouthWest    01-Feb-2002 12:18:00    458.98    1.8202e+06    07-Feb-2002 16:50:00    winter storm   
    SouthEast    23-Jan-2003 00:49:00    530.14    2.1204e+05                     NaT    winter storm   
    SouthEast    07-Feb-2003 21:15:00     289.4    1.4294e+05    17-Feb-2003 08:14:00    winter storm   
    West         06-Apr-2004 05:44:00    434.81    3.4037e+05    06-Apr-2004 06:10:00    equipment fault
    MidWest      16-Mar-2002 06:18:00    186.44    2.1275e+05    18-Mar-2002 23:23:00    severe storm   
    West         18-Jun-2003 02:49:00         0             0    18-Jun-2003 10:54:00    attack         
    West         20-Jun-2004 14:39:00    231.29           NaN    20-Jun-2004 19:16:00    equipment fault
    West         06-Jun-2002 19:28:00    311.86           NaN    07-Jun-2002 00:51:00    equipment fault
    NorthEast    16-Jul-2003 16:23:00    239.93         49434    17-Jul-2003 01:12:00    fire           
    MidWest      27-Sep-2004 11:09:00    286.72         66104    27-Sep-2004 16:37:00    equipment fault
    SouthEast    05-Sep-2004 17:48:00    73.387         36073    05-Sep-2004 20:46:00    equipment fault
    West         21-May-2004 21:45:00    159.99           NaN    22-May-2004 04:23:00    equipment fault
    SouthEast    01-Sep-2002 18:22:00    95.917         36759    01-Sep-2002 19:12:00    severe storm   
    SouthEast    27-Sep-2003 07:32:00       NaN    3.5517e+05    04-Oct-2003 07:02:00    severe storm   
    West         12-Nov-2003 06:12:00    254.09    9.2429e+05    17-Nov-2003 02:04:00    winter storm   
    NorthEast    18-Sep-2004 05:54:00         0             0                     NaT    equipment fault
      ⋮

Merge Cause, Loss, and RestorationTime. Because these variables have different types, merge them into a table that is nested in the output table.

T2 = mergevars(T1,["Cause","Loss","RestorationTime"],...
               "NewVariableName","LossData","MergeAsTable",true)
T2=1468×4 table
     Region           OutageTime         Customers                         LossData                     
                                                            Cause          Loss       RestorationTime   
    _________    ____________________    __________    _________________________________________________

    SouthWest    01-Feb-2002 12:18:00    1.8202e+06    winter storm       458.98    07-Feb-2002 16:50:00
    SouthEast    23-Jan-2003 00:49:00    2.1204e+05    winter storm       530.14                     NaT
    SouthEast    07-Feb-2003 21:15:00    1.4294e+05    winter storm        289.4    17-Feb-2003 08:14:00
    West         06-Apr-2004 05:44:00    3.4037e+05    equipment fault    434.81    06-Apr-2004 06:10:00
    MidWest      16-Mar-2002 06:18:00    2.1275e+05    severe storm       186.44    18-Mar-2002 23:23:00
    West         18-Jun-2003 02:49:00             0    attack                  0    18-Jun-2003 10:54:00
    West         20-Jun-2004 14:39:00           NaN    equipment fault    231.29    20-Jun-2004 19:16:00
    West         06-Jun-2002 19:28:00           NaN    equipment fault    311.86    07-Jun-2002 00:51:00
    NorthEast    16-Jul-2003 16:23:00         49434    fire               239.93    17-Jul-2003 01:12:00
    MidWest      27-Sep-2004 11:09:00         66104    equipment fault    286.72    27-Sep-2004 16:37:00
    SouthEast    05-Sep-2004 17:48:00         36073    equipment fault    73.387    05-Sep-2004 20:46:00
    West         21-May-2004 21:45:00           NaN    equipment fault    159.99    22-May-2004 04:23:00
    SouthEast    01-Sep-2002 18:22:00         36759    severe storm       95.917    01-Sep-2002 19:12:00
    SouthEast    27-Sep-2003 07:32:00    3.5517e+05    severe storm          NaN    04-Oct-2003 07:02:00
    West         12-Nov-2003 06:12:00    9.2429e+05    winter storm       254.09    17-Nov-2003 02:04:00
    NorthEast    18-Sep-2004 05:54:00             0    equipment fault         0                     NaT
      ⋮

Input Arguments

collapse all

Input table, specified as a table or timetable.

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

Name-Value Arguments

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: T2 = mergevars(T1,vars,NewVariableName="MergedResults")

Before R2021a, use commas to separate each name and value, and enclose Name in quotes.

Example: T2 = mergevars(T1,vars,"MergeAsTable",true)

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

Flag to merge variables into a table, specified as a numeric or logical 1 (true) or 0 (false). Set this flag to true or 1 to merge the specified variables into a table that is nested into a variable of the output table. Use this argument to combine variables that cannot be concatenated into an array.

The default value of false causes mergevars to merge the specified variables into one multicolumn array that is a variable of the output table.

Extended Capabilities

Version History

Introduced in R2018a