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.
A key holding a missing value (NaN, NaT,
<undefined> or <missing>) matches no other key. Empty
text, an empty character vector in a cellstr or a blank row of a
character matrix, is a value and matches empty text, where MATLAB
matches it with nothing; ismissing still flags it as missing.
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).
When both inputs have the same name, as when a table is joined with
itself, the left names are numbered as well ('_T_1' beside
'_T'), as in MATLAB.
The row names of tblL, if any, are preserved.
tbl = join (tblL, tblR, Name, Value)
customizes the join with the following options:
'Keys''LeftKeys' or
'RightKeys'.'LeftKeys', 'RightKeys''LeftVariables', 'RightVariables''LeftVariables' is all the variables of tblL and
'RightVariables' is the non-key variables of tblR.'KeepOneCopy' [tbl, ixR] = join (…) also returns the index
vector ixR that identifies, for each row of tbl, the matching
row of tblR.
Source Code: table
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