# Merge tables with different columns and for matching columns update missing values in left table with values from right table

16 views (last 30 days)
Maximilian Schwarz on 22 Dec 2022
I would like to merge two or more tables with different lenght, different and matching column names and one identical key T. Columns present only in the right table should be simply merged to the left table as with outerjoin. For columns present in both tables missing values in the left table should be replaced with nonmissing values from the right table. Is this possible in a single function? (see example below)
A = table([1;2;3;4;5], {'A', 'A', 'A', 'A','A'}', ...
'VariableNames',{'T', 'Var1'})
B = table([4 5 6 7]',{'B' 'B' 'B' 'B'}',...
'VariableNames',{'T' 'Var1'})
C = table([1 2 3]',{'C' 'C' 'C'}',...
'VariableNames',{'T' 'Var2'})
% Merge on T and final output should look like this
Final = table([1 2 3 4 5 6 7]',{'A', 'A', 'A', 'A','A', 'B', 'B'}', {'C' 'C' 'C' 'NaN' 'NaN' 'NaN' 'NaN'}', ...
'VariableNames',{'T','Var1', 'Var2'})

Eric Sofen on 27 Dec 2022
This is a tricky combination, and not doable in a single join right now. As you say, the outerjoin of A and C to create a table with Var1 and Var2 is the easy part. However, we end up with rows with duplicate keys from the outerjoin of A and B. That requires some cleanup. I think the approach I outline below is general enough to tackle problems more involved than the example you gave.
A = table([1;2;3;4;5], {'A', 'A', 'A', 'A','A'}', ...
'VariableNames',{'T', 'Var1'});
B = table([4 5 6 7]',{'B' 'B' 'B' 'B'}',...
'VariableNames',{'T' 'Var1'});
C = table([1 2 3]',{'C' 'C' 'C'}',...
'VariableNames',{'T' 'Var2'});
D = outerjoin(A,B,MergeKeys=true)
D = 9×2 table
T Var1 _ _____ 1 {'A'} 2 {'A'} 3 {'A'} 4 {'A'} 4 {'B'} 5 {'A'} 5 {'B'} 6 {'B'} 7 {'B'}
% Clean up the rows with duplicate keys. Keep the "last" one to get the
% desired overwriting behavior.
[u,ia] = unique(D.T,"last");
D = D(ia,:)
D = 7×2 table
T Var1 _ _____ 1 {'A'} 2 {'A'} 3 {'A'} 4 {'B'} 5 {'B'} 6 {'B'} 7 {'B'}
Final = outerjoin(D,C,MergeKeys=true)
Final = 7×3 table
T Var1 Var2 _ _____ __________ 1 {'A'} {'C' } 2 {'A'} {'C' } 3 {'A'} {'C' } 4 {'B'} {0×0 char} 5 {'B'} {0×0 char} 6 {'B'} {0×0 char} 7 {'B'} {0×0 char}
Maximilian Schwarz on 3 Jan 2023
But this for instance would not work anympore as soon as B and C are combine into one table, right?

### Categories

Find more on Tables in Help Center and File Exchange

R2021b

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!