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

119 ビュー (過去 30 日間)
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'})

回答 (2 件)

Eric Sofen
Eric Sofen 2022 年 12 月 27 日
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}
  1 件のコメント
Maximilian  Schwarz
Maximilian Schwarz 2023 年 1 月 3 日
But this for instance would not work anympore as soon as B and C are combine into one table, right?


Maria Battle
Maria Battle 2024 年 4 月 5 日 19:05
編集済み: Maria Battle 2024 年 4 月 10 日 19:14
table_ab = joindata(A, B);
C.Var1 = string(zeros(height(C), 1)); % explanation below
all_tables = joindata(table_ab, C);
To use joindata(), "The first two columns of both frame tables contain the segment start and segment end points. The other columns in the tables contain data associated with the frame." See help page for more details.
Another example:
t1 = table([1;2;3], [1.1;2.1;3.1], [4;5;6], 'VariableNames', {'index', 'gas', 'electric'});
t2 = table([4;5;6;7], [1;2;3;3.5], [4;5;6;6.5], [7;8;9;9.5], 'VariableNames', {'index', 'gas', 'solar', 'wind'});
t3 = table([8;9], ["fee"; "fi"], ["me"; "you"], ["p1"; "p2"], 'VariableNames', {'index', 'gas', 'hood1', 'hood2'});
Ta = joindata(t1, t2);
Tb = joindata(Ta, t3);


Help Center および File ExchangeTables についてさらに検索




Community Treasure Hunt

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

Start Hunting!

Translated by