3
$\begingroup$

I am using a dataset to construct a matrix, how can I add the title Mass in kilograms above the Earth and Moon like in the table. enter image description here

Here is the code I am working on.

Dataset[
  <|"Earth planet and Moon" ->
      <|"Earth" -> <|"Earth" -> Entity["Planet", "Earth"]["Mass"]^2,
        "Moon" ->
          Entity["PlanetaryMoon", "Moon"]["Mass"] * 
            Entity["Planet", "Earth"]["Mass"]|>,
    "Moon" ->
      <|"Earth" ->
          Entity["PlanetaryMoon", "Moon"]["Mass"] * 
            Entity["Planet", "Earth"]["Mass"], 
        "Moon" -> Entity["PlanetaryMoon", "Moon"]["Mass"]^2|>|>|>]

When I try to make it a data set I only get the "Earth planet and Moon Mass" like the image below. enter image description here

$\endgroup$
5
  • $\begingroup$ Can you please explain what you mean by “just like with the rows”? It does not appear that the code you posted matches to the output you show in the image. $\endgroup$ Commented Aug 10, 2020 at 21:18
  • $\begingroup$ I updated the question. I can only manage to insert the "Earth planet and Moon" to the dataset. How can I add the title "Mass in kilograms" above Earth and Moon. $\endgroup$
    – Las Des
    Commented Aug 10, 2020 at 21:51
  • $\begingroup$ Could be something like this: Dataset@ <| <|"Earth planet and Moon" -> <|"Earth" -> <| <| "Mass in kg" -> <|"Earth" -> 3.57 kg ^2, "Moon" -> 4.387 kg^2|>|>|>, "Moon" -> <|<| "Mass in kg" -> <|{"Earth" -> 4.387 kg^2, "Moon" -> 4.387 kg^2}|>|>|> |> |>|>. $\endgroup$
    – Las Des
    Commented Aug 11, 2020 at 3:02
  • $\begingroup$ I have failed to locate what the question is. I'm confused... $\endgroup$ Commented Aug 11, 2020 at 17:26
  • 1
    $\begingroup$ @GustavoDelfino the question is how to achieve the formatting shown. I am sure it should be possible, but it seemingly is a frustrating organizational format according to Dataset—it is not trivial to show how this is possible. $\endgroup$ Commented Aug 12, 2020 at 8:30

1 Answer 1

6
$\begingroup$

Update

Dataset doesn't seem to give the kind of control over formatting that you want, but Grid does, so I recommend using it for display. You can still use the dataset to store data. ALso, since the key "Earth planet and Moon" only function is to serve as a label, I would remove the 1st level association from you dataset—reverting it back to the way you oringinally posted it. This will simplify references to data values in queries and in the display code.

ds =
  Dataset[
    {<|"Earth" ->
         <|"Earth" -> Entity["Planet", "Earth"]["Mass"]^2, 
           "Moon" -> 
             Entity["PlanetaryMoon", "Moon"]["Mass"] * 
             Entity["Planet", "Earth"]["Mass"]|>, 
       "Moon" -> 
         <|"Earth" -> 
             Entity["PlanetaryMoon", "Moon"]["Mass"] * 
             Entity["Planet", "Earth"]["Mass"], 
           "Moon" -> Entity["PlanetaryMoon", "Moon"]["Mass"]^2|>|>}]

data

I have also made some changes to the grid's options. I think they improve the way it looks.

With[{gray = GrayLevel[.95]},
  Grid[
    {{"", "", "Product of Masses", SpanFromLeft}, 
     {SpanFromAbove, SpanFromAbove, "Earth", "Moon"},
     {"Earth planet\nand Moon", Item["Earth", Frame -> True],
      ds[1, "Earth", "Earth"], ds[1, "Earth", "Moon"]},
     {SpanFromAbove, Item["Moon", Frame -> True],
      ds[1, "Moon", "Earth"], ds[1, "Moon", "Moon"]}},
    BaseStyle -> Directive["SR"],
    Dividers -> {{{True, False, {True}, -1 -> True}}, All},
    Background -> 
      {None, None, {{{1, 4}, {1, 2}} -> gray, {{1, 2}, {3, 4}} -> gray}}]]

grid

$\endgroup$
1
  • $\begingroup$ Seems like this is for now the only way. $\endgroup$
    – Las Des
    Commented Aug 16, 2020 at 17:29

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.