How to vertically join differently sized tables?

5 visualizaciones (últimos 30 días)
HWIK
HWIK el 5 de Ag. de 2021
Comentada: HWIK el 5 de Ag. de 2021
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!

Respuesta aceptada

Peter Perkins
Peter Perkins el 5 de Ag. de 2021
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.
  1 comentario
HWIK
HWIK el 5 de Ag. de 2021
Yes, adding it manually might be best after all, if not the missing values are assigned some default values, not necessarily the ones I'd be looking for. But thanks for the help!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Tables en Help Center y File Exchange.

Productos


Versión

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by