1
$\begingroup$

I have two matrices with different dimensions I want to join them together in one matrix. for example, I have two lists of diseases associated with their symptoms in both matrix, what I want is to join these two lists or matrix in one matrix without lost the observation in each original matrix.

list1 = {{"Dis", "S1", "S2", "S3", "S4", "S5", "S6"},
         {"D1", 1, 1, 0, 1, 1, 0}, {"D2", 1, 0, 1, 0, 0, 1}, {"D3", 0, 0, 1, 1, 0, 1},
         {"D4", 0, 1, 0, 0, 1, 1}, {"D5", 1, 1, 1, 0, 1, 1}, {"D6", 1, 1, 0, 0, 0, 1},
         {"D7", 0, 1, 1, 1, 0, 0}};
List2 = {{"Dis", "S7", "S8", "S4", "S9", "S10", "S3", "S11", "S12", "S5", "S1"},
         {"D2", 0, 1, 0, 0, 1, 0, 1, 1, 0, 0}, {"D8", 1, 1, 1, 1, 1, 0, 0, 0, 1, 0},
         {"D9", 1, 0, 1, 1, 0, 0, 0, 1, 0, 1}, {"D10", 0, 0, 1, 0, 0, 1, 1, 0, 0, 0},
         {"D6", 0, 1, 0, 0, 1, 0, 1, 0, 0, 1}, {"D11", 1, 1, 0, 1, 0, 1, 0, 1, 0, 1},
         {"D12", 0, 1, 0, 1, 0, 1, 0, 0, 0, 0}, {"D13", 1, 0, 1, 0, 1, 1, 0, 0, 0, 1},
         {"D14", 0, 1, 1, 1, 1, 0, 1, 1, 1, 1}, {"D15", 0, 1, 0, 0, 0, 0, 1, 0, 0, 0},
         {"D16", 0, 0, 0, 1, 0, 0, 0, 0, 1, 1}, {"D17", 1, 0, 1, 0, 0, 0, 1, 1, 0, 0}};

in order to get output like that:

{{"Dis", "S1", "S2", "S3", "S4", "S5", "S6", "S7", "S8", "S9", "S10", 
  "S11", "S12"}, {"D1", 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0}, {"D2", 1,
   0, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1}, {"D3", 0, 0, 1, 1, 0, 1, 0, 0, 0,
   0, 0, 0}, {"D4", 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0}, {"D5", 1, 1, 
  1, 0, 1, 1, 0, 0, 0, 0, 0, 0}, {"D6", 1, 1, 0, 1, 1, 1, 0, 1, 0, 0, 
  0, 1}, {"D7", 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0}, {"D8", 1, 1, 1, 
  1, 1, 0, 1, 0, 1, 1, 0, 0}, {"D9", 1, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 
  1}, {"D10", 0, 0, 1, 0, 0, 1, 1, 0, 0, 0}, {"D6", 0, 1, 0, 0, 1, 0, 
  1, 0, 0, 1}, {"D11", 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1}, {"D12", 0,
   0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0}, {"D13", 1, 0, 1, 4, 0, 0, 1, 0, 
  0, 1, 0, 0}, {"D14", 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1}, {"D15", 0,
   0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0}, {"D16", 0, 0, 0, 0, 1, 0, 0, 0, 
  1, 0, 0, 0}, {"D17", 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 1}}

thank you for any insight

$\endgroup$
11
  • 1
    $\begingroup$ Can you explain a little better what exact operation you are trying to do? I don't understand what way you would like to combine this data. $\endgroup$
    – Carl Lange
    Commented May 21, 2019 at 21:32
  • 1
    $\begingroup$ Significantly related (possible duplicates?) by same user: mathematica.stackexchange.com/questions/195340/… and mathematica.stackexchange.com/questions/194840/…. $\endgroup$
    – Carl Lange
    Commented May 21, 2019 at 21:35
  • 1
    $\begingroup$ OK, but this doesn't yet explain how to merge these datasets. Can you explain how you would do it "on paper", step-by-step? (Preferably, edit your question with these details) $\endgroup$
    – Carl Lange
    Commented May 21, 2019 at 21:37
  • 1
    $\begingroup$ Are the questions not related? The extra context from your past questions might help future answerers, since this question doesn't have a lot of detail in it. Further - I'm sure there is a way to do what you're looking for, but it's difficult for me to understand precisely what it is you're looking for. Perhaps another user will be able to help. $\endgroup$
    – Carl Lange
    Commented May 21, 2019 at 21:42
  • 1
    $\begingroup$ This is a non-traditional math. $\endgroup$
    – user64494
    Commented May 22, 2019 at 3:33

1 Answer 1

3
$\begingroup$

Here is the first thing I came up with, but unfortunately it's a little gross, and I'm not exactly sure what you're looking for. I'm afraid I can't spend more time on this question so hopefully someone else can flesh this out.

First let's transform the data into Datasets. This conversion is only the first gross part.

d1 = ImportString[ExportString[list1, "CSV"], "Dataset", 
  HeaderLines -> {1, 1}]
d2 = ImportString[ExportString[list2, "CSV"], "Dataset", 
  HeaderLines -> {1, 1}]

and now we can join the data:

Join[d1, d2]

enter image description here

We can replace the Missing values with 0 like so:

d = Transpose@Transpose@Join[d1, d2] /. _Missing -> 0

enter image description here

(We must Transpose so that the underlying Association actually has the Missing values to replace, rather than just short Associations. Check out the Normal of the dataset to see what I mean.)

You can convert back to your original structure with:

ImportString[ExportString[d, "CSV"], "CSV"]
{{"", "S1", "S2", "S3", "S4", "S5", "S6", "S7", "S8", "S9", "S10", 
  "S11", "S12"}, {"D1", 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0}, {"D2", 0,
   0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1}, {"D3", 0, 0, 1, 1, 0, 1, 0, 0, 0,
   0, 0, 0}, {"D4", 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0}, {"D5", 1, 1, 
  1, 0, 1, 1, 0, 0, 0, 0, 0, 0}, {"D6", 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 
  1, 0}, {"D7", 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0}, {"D8", 0, 0, 0, 
  1, 1, 0, 1, 1, 1, 1, 0, 0}, {"D9", 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 
  1}, {"D10", 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0}, {"D11", 1, 0, 1, 0,
   0, 0, 1, 1, 1, 0, 0, 1}, {"D12", 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 
  0}, {"D13", 1, 0, 1, 1, 0, 0, 1, 0, 0, 1, 0, 0}, {"D14", 1, 0, 0, 1,
   1, 0, 0, 1, 1, 1, 1, 1}, {"D15", 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 
  0}, {"D16", 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0}, {"D17", 0, 0, 0, 1,
   0, 0, 1, 0, 0, 0, 1, 1}}

This answer contains a lot of bad code and I would recommend someone else add a better answer.

$\endgroup$
4
  • $\begingroup$ Carl that so helpfull need only all the col {} empty replaced by 0 thank you so much $\endgroup$
    – Ben Aawf
    Commented May 21, 2019 at 22:09
  • $\begingroup$ @BenAawf See my edit. $\endgroup$
    – Carl Lange
    Commented May 21, 2019 at 22:17
  • $\begingroup$ thank you so much, #Carl Lange for your insight but this output does not help as a matrix. $\endgroup$
    – Ben Aawf
    Commented May 21, 2019 at 23:11
  • $\begingroup$ @BenAawf See my edit. $\endgroup$
    – Carl Lange
    Commented May 21, 2019 at 23:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.