table

Methods

Method Reference: table.join

table: tbl = join (tblL, tblR)
table: tbl = join (tblL, tblR, Name, Value)
table: [tbl, ixR] = join (…)

Combine two tables by rows using key variables.

tbl = join (tblL, tblR) combines tblL and tblR by matching the values of their key variables, which by default are the variables that share the same name in both tables. tbl contains one row for each row of tblL, in the same order; each is completed with the single row of tblR whose key variables match. The key variables of tblR must contain unique combinations of values, and every key combination in tblL must be present in tblR.

By default tbl contains all the variables of tblL followed by the non-key variables of tblR. Whenever a non-key variable name appears in both tables, a suffix derived from each input’s argument name is appended to the conflicting names (for inputs named tblL and tblR, the suffixes '_tblL' and '_tblR'; when an input has no name, '_left' and '_right' are used). The row names of tblL, if any, are preserved.

tbl = join (tblL, tblR, Name, Value) customizes the join with the following options:

'Keys'
Variables to use as keys in both tables, given as variable names or indices. It cannot be combined with 'LeftKeys' or 'RightKeys'.
'LeftKeys', 'RightKeys'
Variables to use as keys in tblL and tblR, respectively, when the key variables have different names. They must be specified together and reference the same number of variables.
'LeftVariables', 'RightVariables'
Variables of tblL and tblR to include in tbl. By default 'LeftVariables' is all the variables of tblL and 'RightVariables' is the non-key variables of tblR.
'KeepOneCopy'
Names of non-key variables that occur in both tables for which only the copy from tblL is kept (no suffix is added and the tblR copy is dropped).

[tbl, ixR] = join (…) also returns the index vector ixR that identifies, for each row of tbl, the matching row of tblR.

Source Code: table

Example: 1

join performs a key-based merge: every row of the left table is matched to the one row of the right table sharing its key. The key is the variable the two tables have in common (Name), and it must be unique on the right. The remaining right-hand variables — here of several different types — are appended to each matched left row.

 L = table ({'Li'; 'Diaz'; 'Brown'}, [38; 40; 49], ...
            'VariableNames', {'Name', 'Age'})
L =
  3x2 table

      Name       Age    
    _________    ___    

    {'Li'   }     38    
    {'Diaz' }     40    
    {'Brown'}     49
 R = table ({'Li'; 'Diaz'; 'Brown'}, categorical ({'NY'; 'CA'; 'MA'}), ...
            logical ([1; 0; 1]), ...
            'VariableNames', {'Name', 'State', 'Smoker'})
R =
  3x3 table

      Name       State    Smoker    
    _________    _____    ______    

    {'Li'   }       NY    true      
    {'Diaz' }       CA    false     
    {'Brown'}       MA    true
 join (L, R)
ans =
  3x4 table

      Name       Age    State    Smoker    
    _________    ___    _____    ______    

    {'Li'   }     38       NY    true      
    {'Diaz' }     40       CA    false     
    {'Brown'}     49       MA    true