How to vertically join differently sized tables?
3 次查看(过去 30 天)
显示 更早的评论
Hi,
Lets say I have two tables:
table1=table(1,70,175,29,datetime(1992,7,1),"M",'VariableNames',{'Num','Weight','Height','Age','Birth','Gender'})
table2=table(2,175,29,'VariableNames',{'Num','Height','Age'})
Where table1 has 6 variables and table2 has 3, but all 3 of those appear in table1. Would it be possible to vertically concatenate these two and fill the missing variables in table2 with NaN or NaT for doubles and datetimes?
Any help is appreciated,
Thanks!
0 个评论
采纳的回答
Peter Perkins
2021-8-5
One way is to "assign off the end":
>> table1 = table(1,70,175,29,datetime(1992,7,1),"M",'VariableNames',{'Num','Weight','Height','Age','Birth','Gender'})
table2 = table(2,175,29,'VariableNames',{'Num','Height','Age'})
table1 =
1×6 table
Num Weight Height Age Birth Gender
___ ______ ______ ___ ___________ ______
1 70 175 29 01-Jul-1992 "M"
table2 =
1×3 table
Num Height Age
___ ______ ___
2 175 29
>> table1((end+1):(end+height(table2)),table2.Properties.VariableNames) = table2
Warning: The assignment added rows to the table, but did not assign values to all of the table's existing variables. Those variables are extended with rows containing
default values.
> In tabular/subsasgnParens (line 482)
In tabular/subsasgn (line 64)
table1 =
2×6 table
Num Weight Height Age Birth Gender
___ ______ ______ ___ ___________ _________
1 70 175 29 01-Jul-1992 "M"
2 0 175 29 NaT <missing>
But that is perhaps not the most controlled way to do it. You might be better off just adding the missing variables to the second table.
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!