Since the question stems from working with matrices with named rows and columns I think the answer below is relevant. My answer does not use or deal with Dataset objects, but with sparse matrices with named rows and columns.
In the last few years I have used a lot the R base library Matrix that has implementation of sparse matrix objects and efficient computations. To the sparse matrices from the Matrix R library one can assign and retrieve row and column names with the functions colnames
and rownames
. Sometimes I miss this in Mathematica so I started a Mathematica package that implements similar functionalities. The package does not use RLink, it has purely Mathematica language implementations.
The RSparseMatrix
objects of this package can deal with the permutations of row and column names. (Note that the package is not finished and in some functions the row and column names are ignored.)
This document has demos/tests. (Arithmetic operations, matrix products, joining, profiling, etc.)
This blog post has a neat application example.
Load
This loads the package from GitHub:
Import["https://raw.githubusercontent.com/antononcube/MathematicaForPrediction/master/Misc/RSparseMatrix.m"]
Creation
Create a RSparseMatrix
object with array rules (like for SparseArray
) and names of rows, columns, and dimensions:
rmat = MakeRSparseMatrix[{{1, 1} -> 1, {2, 2} -> 2, {4, 3} -> 3, {1, 4} -> 4},
"ColumnNames" -> {"a", "b", "c", "d"}, "RowNames" -> {"A", "B", "C", "D"},
"DimensionNames" -> {"U", "V"}];
Show the created matrix:
rmat // MatrixForm

The creation can be done with a SparseArray
object:
rmat = ToRSparseMatrix[SparseArray[rmat],
"ColumnNames" -> {"a", "b", "c", "d"}, "RowNames" -> {"A", "B", "C", "D"},
"DimensionNames" -> {"U", "V"}];
rmat // MatrixForm
Permutations
Here we permute the matrices:
MatrixForm[rmat[[RotateLeft[RowNames[rmat], 2]]]]

MatrixForm[rmat[[All, RotateLeft[ColumnNames[rmat], 2]]]]

MatrixForm[
rmat[[RotateLeft[RowNames[rmat], 2], RotateLeft[ColumnNames[rmat], 2]]]]

Converting to SparseArray
At some point it is better to convert to an inherent data structure of Mathematica:
SparseArray[rmat[[All, RotateLeft@ColumnNames[rmat]]]] // MatrixForm

Merge[Normal/@{ds1, ds2}, Merge[#, Identity] &] // Dataset
$\endgroup$Merge[Merge[Identity]] @ Map[Normal] @ {ds1, ds2}
$\endgroup$Merge
would be to allow level specifications. I repeatedly find myself using something likeNest[Merge, fun, n]
wheren
is the desired depth andfun
is the desired lowest-level merging function. $\endgroup$