How to combine table vertically

78 visualizaciones (últimos 30 días)
Wendy Cameron
Wendy Cameron el 24 de Nov. de 2018
Comentada: Wendy Cameron el 27 de Nov. de 2018
I have two large tables I want to combine. They both have exactly the same column headings but different row numbers (>300) and the headings may be numbers, categoricals or text. I just want to combine them vertically so that the final table has just one column heading with all the data underneath.
  2 comentarios
KALYAN ACHARJYA
KALYAN ACHARJYA el 24 de Nov. de 2018
outerjoin(table1,table2,'MergeKeys', true);
Ans Link
Wendy Cameron
Wendy Cameron el 24 de Nov. de 2018
Thank you - this answer worked. I had been trying to use vertcat which works for an array and although one could convert the table to an arry as below, this solution here seems to work nicely in my situation. Thank you all very much or your ongoing assistance with Matlab.
Kind regards, Wendy

Iniciar sesión para comentar.

Respuesta aceptada

madhan ravi
madhan ravi el 24 de Nov. de 2018
Editada: madhan ravi el 24 de Nov. de 2018

Más respuestas (1)

Peter Perkins
Peter Perkins el 27 de Nov. de 2018
Wendy, I'm not sure you are getting the correct advice. You have said you want to vertically concatenate. outerjoin does not do that. It's a completely different operation, closer to horizontal concatenation.
If you do want to vertically concatenate, you do that the same way with tables as with anything else in MATLAB: [t1; t2]. No conversion needed. However, the variable names in the two tables do need to be the same. You have said, "They both have exactly the same column headings", so that should not be a problem. You also said, "the headings may be numbers, categoricals or text.", which doesn't make sense, so I think maybe you meant to say, "the variables in the table may be ...".
So, if you want to vertically concatenate, you shouldn't use outerjoin, and you don't need to convert back and forth with numeric.
  3 comentarios
Peter Perkins
Peter Perkins el 27 de Nov. de 2018
I almost take back part of what I said about outerjoin. outerjoin happens to work in this case, because
1) you have identical variable names in both tables, and the default behavior is to use common vars as keys,
2) there's the flag to merge the keys in the output, and
3) apparently the two tables have no rows in common, so the outer join doesn't actually join anything, it treats every row as unmatched.
But this is pretty obscure. It will not work correctly as soon as the two tables have a row in common, and it isn't very obvious. A join is fundamentally a horzcat-like operation. What went wrong with vertcat?
On the other hand, if what you really want is, "vertcat the two tables but only keep one copy of common rows", then perhaps outerjoin is what you want. I would argue that vertcat+unique is more understandable, but that's up to you.
Hope this helps.
Wendy Cameron
Wendy Cameron el 27 de Nov. de 2018
Great, thanks for the feedback Peter. It seems it worked just out of luck for my situation. I checked a few columns to be sure. But your method works too and is of course simpler and I have noted that down to use next time. Thanks once again for helping me to better understand matlab.

Iniciar sesión para comentar.

Categorías

Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by