How to combine table vertically
77 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
更多回答(1 个)
Peter Perkins
2018-11-27
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 个评论
Peter Perkins
2018-11-27
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!