MATLAB Answers


Eliminating duplicate columns in a table when appending

Danielle Leblance さんによって質問されました 2017 年 2 月 5 日
最新アクティビティ the cyclist
さんによって コメントされました 2017 年 2 月 5 日
I am importing data using readtable function. but when i append data using:
mytable=[mytable mytable2];
I receive an error: Duplicate variable name: 'ID' because every file contains the ID of the firm. I need the column ID only once. so how can i skip the ID column in mytable2 when i want to append?

  0 件のコメント

サインイン to comment.


1 件の回答

the cyclist
回答者: the cyclist
2017 年 2 月 5 日
編集済み: the cyclist
2017 年 2 月 5 日

Use the join command. Here's a simple example.
% Set up some sample data
id = [1 2 3]';
firstName = {'Walter','Image','Star'}';
lastName = {'Roberson','Analyst','Strider'}';
tableFirst = table(id,firstName);
tableLast = table(id,lastName);
% Join the tables
tableFirstAndLast = join(tableFirst,tableLast)
FYI, there are many input options that govern how the variables in the two tables are retained when there are duplicates, so I suggest carefully reading the documentation (linked above) if this example doesn't do exactly what you want in your case.
You might also need a different function from that family, e.g. innerjoin.

  5 件のコメント

Danielle Leblance 2017 年 2 月 5 日
thanks. Attached the file. I have 45 tables and i want to merge them in one. the number of rows is the same and in the same order. So how can I append vertically all these tables? I was merging two by two using worked for some and didn't work for others. I removed most of the tables in order to upload the file since there is a 5m cap
Danielle Leblance 2017 年 2 月 5 日
attached the file
the cyclist
2017 年 2 月 5 日
The proximate cause is that some of the tables contain a row with NaN as the value of IDRSSD. Because one NaN is not equal to another NaN, MATLAB cannot join those rows.
One simple solution might be to replace those values of NaN with some other unique identifier. (It looks like setting IDRSSD=0 would work.)
You could also use the innerjoin() function, as I mentioned earlier, which will only use matching rows.
Or you could manually avoid that first row for those tables:
a2 = join(t7(2:end,:),t8(2:end,:))
but that seems pretty inelegant.
The approach to take will depend on exactly what output you want, and what is more readable and efficient.

サインイン to comment.

Translated by