I have two different datasets : dsDat1
and dsDat2
:
ClearAll[dat1, dat2, col1, col2, row1, row2, dsDat1, dsDat2];
SeedRandom[123];
dat1 = RandomInteger[5, {10, 5}];
col1 = {"BOS", "labor", "revenue", "worker", "share"};
row1 = {"agr", "min", "ma1", "ma3", "ma5", "soth", "sbus", "stel",
"segw", "sfin"};
dsDat1 =
Dataset@AssociationThread[row1, AssociationThread[col1, #] & /@ dat1]
dat2 = RandomInteger[5, {10, 5}];
col2 = {"Numb1", "Tot", "Rev", "Per", "Share"};
row2 = {"min", "agr", "ma5", "ma3", "ma1", "stel", "soth", "sbus",
"segw", "sdummy"};
dsDat2 =
Dataset@AssociationThread[row2, AssociationThread[col2, #] & /@ dat2]
I want to create a single unified
dataset by combining the two different datasets using the row keys in dsDat1
as a reference. There are some complications though. The second dataset dsDat2
is not perfectly matching
dsDat1
in terms of row keys.
Here are the places they do not match:
- The order of the row keys in
dsDat2
is mixed compared to those indsDat1
; - Some row keys in
dsDat2
do not exist indsDat1
, and some row keys indsDat1
do not exist indsDat2
;
I like to handle those cases in items 1 and 2 as follows:
- Consider the row keys in
dsDat1
as fixed, and create a new row at the end of theunified
dataset for each row key that does not exist indsDat1
but indsDat2
; - For the missing row keys in
dsDat2
(compared todsDat1
), placeMissingValue[]
in the unified dataset;
Basically, I just want to create a unified dataset by combining two different datasets.