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)
Show older comments
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'})
0 Comments
Answers (1)
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)
% 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,:)
Final = outerjoin(D,C,MergeKeys=true)
See Also
Categories
Find more on Tables in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!