How do i combine many tables by respective column content?

3 次查看(过去 30 天)
Dear Experiences ...
i have three tables ( table A, table B and table C)
table A look like following where first columns contain unique names, and second column contain public names (may contain repeated names)..
name sun_name
____________
Amy 'tomy'
Bob 'tomy'
Hol 'salmon'
Har 'cookies'
Sal 'pizza'
second column B look like following
sun_name var1(B).....varn(B)
____________ _______________
'tomy' 0.0 .... 4.5
'tomy' 4.5 .... 5.2
'salmon' etc..
'cookies'
'pizza'
table C look like following
sun_name var1(c).....varm(c)
____________ _______________
'tomy' 0.0 .... 4.5
'tomy' 4.5 .... 5.2
'salmon' etc..
'cookies'
'pizza'
result table (out) perhaps include all columns .. as follow: out
name sun_name var1(B)...varn(B) var1(C)...varm(C)
the problem here content of name field in table A must be repeated based on other tables ? how to do that please.

采纳的回答

Andrei Bobrov
Andrei Bobrov 2017-4-3
编辑:Andrei Bobrov 2017-4-3
Maybe so?
Table_out = [A,B(:,2:end),C(:,2:end)];
Added:
As said by Guillaume about innerjoin:
A = readtable('1.xls');
B = readtable('2.xls');
C = readtable('3.xls');
Tout = sortrows(innerjoin(A,innerjoin(B,C)),1);

更多回答(1 个)

Guillaume
Guillaume 2017-4-3
I don't see how your excel tables relate to your original question. There's no repeated key in any of them.
Going back to your original question, it looks like you want innerjoin. I'm not too clear on the relationship between B and C. Are you supposed to have 4 'tomy' after joining these two? Or do B and C have the same number of row and must simply be concatenated:
  • option 1: innerjoin
D = innerjoin(B, C);
  • option 2: plain concatenation (probably?)
D = [B, C(:, 2:end)];
The join with A is an innerjoin
A = table({'Amy'; 'Bob'; 'Hol'; 'Har'; 'Sal'}, {'tomy'; 'tomy'; 'salmon'; 'cookies'; 'pizza'}, 'VariableNames', {'name', 'sun_name'})
B = table({'tomy'; 'tomy'; 'salmon'; 'cookies'; 'pizza'}, [0; 4.5; 2; 3; 4], 'VariableNames', {'sun_name', 'var1b'})
C = table({'tomy', 'tomy', 'salmon', 'cookies', 'pizza'}', (10:10:50)', 'VariableNames', {'sun_name', 'var1c'})
D = innerjoin(A, [B, C(:, 2:end)])
results in:
7×4 table
name sun_name var1b var1c
_____ _________ _____ _____
'Har' 'cookies' 3 40
'Sal' 'pizza' 4 50
'Hol' 'salmon' 2 30
'Amy' 'tomy' 0 10
'Amy' 'tomy' 4.5 20
'Bob' 'tomy' 0 10
'Bob' 'tomy' 4.5 20

类别

Help CenterFile Exchange 中查找有关 Language Fundamentals 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by