Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I am working with square (non-symmetric) matrices with named rows and columns. The data I receive is sometimes messy and rows/columns may come in various permutations. I usually need to semi-manually put every matrix into a canonical permutation before I can work with it. I'd rather never have to think about permutations and not even define a canonical one, just index the matrices using the names of their rows/columns.

I was hoping that Dataset would be helpful here, and I made Dataset objects like these:

ds1 = Dataset[<|"a" -> <|"a" -> 1, "b" -> 2|>, "b" -> <|"a" -> 3, "b" -> 0|>|>]

Mathematica graphics

ds2 = Dataset[<|"a" -> <|"b" -> 1, "a" -> 5|>, "b" -> <|"b" -> 1, "a" -> 2|>|>] (* I used a different permutation on purpose *)

Mathematica graphics

What is the simplest way to merge these together to obtain the following?

Dataset[<|"a" -> <|"a" -> {1, 5}, "b" -> {2, 1}|>, "b" -> <|"a" -> {3, 2}, "b" -> {0, 1}|>|>]

Mathematica graphics

Instead of merging with List I will usually need to merge with other operations, e.g. Plus. Once I've built a dataset with List it's easy to do that though.

share|improve this question
    
To clarify, I'm asking if there's a nice and simple way to do this. I can do it in an ugly way. I can then package up that ugly way into a merging function and just use it. But then Dataset didn't help me too much, I might as well have created my own optimized labelled table data structure. –  Szabolcs Aug 5 at 22:42
2  
Is this simple enough? Merge[Normal/@{ds1, ds2}, Merge[#, Identity] &] // Dataset –  Rojo Aug 5 at 22:48
3  
@Rojo if you use operator forms, it can get (in my view) a bit cleaner: Merge[Merge[Identity]] @ Map[Normal] @ {ds1, ds2} –  Taliesin Beynon Aug 5 at 22:54
1  
@Szabolcs I'm deliberately waiting to see what use cases are most common before I devise a general Dataset-merging operation. Feel free to propose something if you think you've nailed the semantics of what such a thing should be. –  Taliesin Beynon Aug 5 at 23:00
1  
@Rojo nice. But the tricky part here will be dealing with intermediate levels that aren't associations. –  Taliesin Beynon Aug 5 at 23:01

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.