Join two table variables into one. == does not works of table type
조회 수: 2(최근 30일)
I have two table variables, and would like to join them.
After comparing 1st column to 7th column of df3 and df4 variables,
if the row from 1st to 7th column is exactly same, then I hope to add the test2 column of df4
into dfcombine variable.
Please give me some helps.... == does not works of table type.
dfcombine = df3;
ind = find(df3(i,1:7)==df4(:,1:7));
dfcombine(i,9) = df4(ind,8);
I hope to make the dfcombine variable as below picture.
Karim 2022년 9월 22일
I think one of the issues is the nan in the data, the result of nan == nan is something for philosophers :)
Anyhow, see below for a procedure to merge the tables as desired. The idea is to ignore the nan and empty values during the comparison.
% have a look at the content of df3
% have a look at the content of df4
% use ismember function to check if the
[df4_check, df3_idx] = ismember( df4(:,[ 1 2 3 4 5]) , df3(:,[ 1 2 3 4 5]) );
% create a copy of the table, add a variable 'test2' filled with nan
test2 = nan(size(df3,1),1);
dfcombine = addvars(df3, test2);
% fill in the test values from df3
dfcombine(df3_idx,end) = df4(df4_check,end);
% have a look at the combined table
Walter Roberson 2022년 9월 22일
You are doing () indexing on a table. The result would be a table row. You compare that to something else that is probably a table. But two tables can only compare if the variable names are the same... which we do not know to be true.
HYEONSEOK SEOK 2022년 9월 22일
For comparing something with logical, table need to change to array
see as below:
clear all ; close all; clc;
for i = 1:height(df3)
for j = 1:7
for k = 1:height(df4)
if sum(j == [5,6])
if sum(sum(checker,2) > 5) % if no nan value - change to 6
test2(i,1) = df4((sum(checker,2) > 5),8);
dfcombin = [df3,test2];