Message Boards Message Boards

0
|
148 Views
|
5 Replies
|
0 Total Likes
View groups...
Share
Share this post:

Dataset output is not as expected

Posted 3 days ago

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

POSTED BY: Karl Fraser
5 Replies
Posted 3 days ago

The "group" header doesn't add anything semantically relevant. Are you doing this purely for display purposes?

The simplest thing that is semantically equivalent would be just:

Join[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|>|>|> *)

Or wrapping that with Dataset:

enter image description here

If you really want the "group", you could do something like this:

<|"group" -> #|> & /@ Join[data]

or with Dataset:

Dataset[<|"group" -> #|> & /@ Join[data]]

enter image description here

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.

POSTED BY: Eric Rimbey
Posted 3 days ago

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.

POSTED BY: Karl Fraser

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.

POSTED BY: Gustavo Delfino

Your data variable seems to be missing a |> just before the "Closed" key. And what is data["on"] ? There is no "on" key in the data association.

POSTED BY: Gustavo Delfino
Posted 3 days ago

Hi, I have corrected the "on/off" key and the missing |> in data now (last minute edits). But the final dataset still generates a duplicate row?

POSTED BY: Karl Fraser
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract