How to replace values in one table with those of another table (different size) under multiple conditions?

4 views (last 30 days)
I created 2 example Tables to show the problem (see picture, Table1 is left, Table2 is right, code of the tables is below).
I want to replace missing values in Table 1 by specific values of Table 2. FIN=Finland (in year 1 and 2) and BEL=Belgium (in year 1) have missing values and the goal is to replace them by EU values with corresponding exportingpartner-industry-year combinations.
E.g., the two BEL-AUT-industry-year1 combinations should be replaced by the EU-AUT-industry-year1 combinations, also the FIN-AUT-industry-year1 combinations should be replaced by the same EU-AUT-industry-year1 combinations (and also for year2).
Conceptually I thought of: If an importing country of Table 1 belongs to the EU (e.g. BEL or FIN) and has NaN at an exporting-industry-year combination, then take from Table 2 the EU as importing country and search for the corresponding exporting-industry-year value and replace the NaN in Table1 by it.
Unfortunately I don't have any idea how to implement it.
Thank you in advance! Also hints are welcomed!
% Create the example Table 1
Countries={'AUT','BEL','CHN','FIN','USA'}';
Importing=repelem(Countries,2*5,1);
Exporting=repmat(repelem(Countries,2,1),(2*5*5)/(2*5),1);
Industry=repmat((1:2)',25,1);
tarrif_year1=rand(50,1)
tarrif_year2=rand(50,1)
Table1=[array2table(Importing),array2table(Exporting),array2table(Industry),array2table(tarrif_year1),array2table(tarrif_year2)];
Table1.Importing=categorical(Table1.Importing);
Table1.Exporting=categorical(Table1.Exporting);
Table1.tarrif_year1(Table1.Importing=='BEL',:)=NaN;
Table1.tarrif_year2(Table1.Importing=='FIN',:)=NaN;
Table1.tarrif_year1(Table1.Importing=='FIN',:)=NaN;
Table1.tarrif_year1(Table1.Importing==Table1.Exporting)=0;
Table1.tarrif_year2(Table1.Importing==Table1.Exporting)=0;
% Create the example Table 2
Countries={'AUT','CHN','EU','USA'}';
Importing=repelem(Countries,2*4,1);
Exporting=repmat(repelem(Countries,2,1),(2*4*4)/(2*4),1);
Industry=repmat((1:2)',16,1);
tarrif_year1=rand(32,1)
tarrif_year2=rand(32,1)
Table2=[array2table(Importing),array2table(Exporting),array2table(Industry),array2table(tarrif_year1),array2table(tarrif_year2)];
Table2.Importing=categorical(Table2.Importing);
Table2.Exporting=categorical(Table2.Exporting);
Table2.tarrif_year1(Table2.Importing==Table2.Exporting)=0;
Table2.tarrif_year2(Table2.Importing==Table2.Exporting)=0;

Answers (1)

Peter Perkins
Peter Perkins on 19 Apr 2018

I'm not 100% sure what result you want, but it seems that you will want to

  1. find the rows in table1 with missing values. this may be as simple as calling rows = any(ismissing(table1(:,someVars)),2).
  2. find the rows in table2 where you want to get the data from. I imagine this will be some kind of ismember on table1(rows,{'Exporting' 'Industry' 'Year'}) and table2(:,{'Exporting' 'Industry' 'Year'}) to get the correspondence.
  3. assign one to the other

ismember on tables is the thing to leverage her, I think.

Categories

Find more on Tables in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!