How to combine table vertically
77 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Wendy Cameron
am 24 Nov. 2018
Kommentiert: Wendy Cameron
am 27 Nov. 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 Kommentare
Akzeptierte Antwort
Weitere Antworten (1)
Peter Perkins
am 27 Nov. 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 Kommentare
Peter Perkins
am 27 Nov. 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.
Siehe auch
Kategorien
Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!