Hi Folks, Not sure if this will format correctly, but. Mathematica newbie here. Given :
data = <|"on" -> <|"A" -> <|Length -> 19, Total -> 8|>,
"B" -> <|Length -> 19, Total -> 8|>,
"C" -> <|Length -> 7, Total -> 0|>|>,
"off" -> <|"A" -> <|Length -> 9, Total -> 42|>,
"B" -> <|Length -> 7, Total -> 11|>,
"C" -> <|Length -> 9, Total -> 27|>|>|>;
(*Extract the columns (A,B,C,...)*)
columns = Keys[data["on"]];
(*Extract the data for "on" and "off"*)
extractRowData[status_] :=
Map[Function[
column, {data[status][column][Length],
data[status][column][Total]}], columns];
(*Construct the dataset with hierarchical columns*)
finalDataset = Dataset[
Association[
"on" ->
AssociationThread[columns, extractRowData["on"]],
"off" ->
AssociationThread[columns, extractRowData["off"]]]][All,
AssociationThread["group",
Map[Function[column,
Association["Length" -> #[[1]],
"Total" -> #[[2]]]], #1] & /@ # &]]
The finalDataset duplicates the Length, Total values row such that there are two row for on/off where there should be only one row for on/off. Looking at the finalDataset via //Normal show that the Length,Total element bit is being duplicated, but I cannot track where this happens though I suspect something in my “&/@#&” bit but its confusing me presently. Any help much appreciated. Thanks
Could you also add the exact output that you want to achieve? I am not sure of what you want to do, but I am sure it is easier than what you are doing.
If you really want the Length and Total headers to be at at the top under group, then it would take me more time than I’m willing to spend fiddling with it. Dataset has very strong “opinions” on how things should be displayed. Maybe just transposing Length-Total with A-B-C would work. If your main concern is just display, then I’d use some other structure, maybe even just Grid.
Thanks guys, this is helpful. I have the Dataset as you show, but went down the rabbit hole of nicer display. I may checkout this Grid structure, thanks again.