6
$\begingroup$

After searching a lot from this forum and @Anton Anton's codes on Cross Tabulation, i still cannot find the way to create a dataset table with two-row headers. The first row has two header Keys: First and Last, and the second row header is {a, b, c, d} as shown in the following dataset:

dataset = Dataset[{
 <|"a" -> 1, "b" -> "x", "c" -> {1}, "d" -> {1}|>,
 <|"a" -> 2, "b" -> "y", "c" -> {2, 3}, "d" -> {1}|>,
 <|"a" -> 3, "b" -> "z", "c" -> {3}, "d" -> {1}|>,
 <|"a" -> 4, "b" -> "x", "c" -> {4, 5}, "d" -> {1}|>,
 <|"a" -> 5, "b" -> "y", "c" -> {5, 6, 7}, "d" -> {1}|>,
 <|"a" -> 6, "b" -> "z", "c" -> {}, "d" -> {1}|>}]

Can some show me how to create a dataset table in which the key First should be above a and b together and the key Last be above c and d together?

EDIT Actually, no need for an image of the desired output. Here are the header Keys:

(*top row header*)
head1 = {"First", "Last"} (* where its elements are defined as *)
"First" -> {"a", "b"}
"Last" -> {"c", "d"}

(*second row header under the top row*)
head2={"a", "b","c", "d"}

EDIT 1 Here is an image of the format I am after.

enter image description here

$\endgroup$
7
  • $\begingroup$ @rhermans: Since I cannot create an input form Code for showing the desired output format, I will edit my question with an image of the desired format. $\endgroup$ Jan 31 at 14:56
  • $\begingroup$ @rhermans: In your comment, you are asking for the Code I am after. I cannot put the desired output in Raw Input Form but I edited the question with extra info. $\endgroup$ Jan 31 at 15:11
  • $\begingroup$ @rhermans: My question is really a simple one (from my perspective). Sorry that it is not clear to readers. I just want the dataset given in the question to have two rows of Keys. In the top row, there are two variables (First and Last) and in the second row, four Keys are distributed across First and Last in the way shown in EDIT. $\endgroup$ Jan 31 at 15:20
  • 1
    $\begingroup$ Like in here Dataset[Take[ExampleData[{"Dataset", "Planets"}], All, -2], DatasetTheme -> "Detailed"] with the Moons? $\endgroup$
    – rhermans
    Jan 31 at 15:24
  • 1
    $\begingroup$ I think all the necessary details have been provided and this question should remain open. I would like to see other ways to achieve the same output. $\endgroup$
    – rhermans
    Jan 31 at 15:56

1 Answer 1

8
$\begingroup$

Solution

The elements with the Key "First" and "Last" must be an Associations with the desired keys, like in

<| "Header"-> <| "key" -> "value" |> |>

Rebuilding by hand

dataset[All, 
    <|
        "First" -> <| "a" -> #a, "b" -> #b |> ,
        "Last"  -> <| "c" -> #c, "d" -> #d |> 
    |>&
]

Using KeyTake

dataset[All, 
    <|
        "First" -> KeyTake[ {"a", "b"} ][#],
        "Last"  -> KeyTake[ {"c", "d"} ][#]
    |>&
]

By position using Take

dataset[All, 
    <|
        "First" -> Take[ #,  2 ],
        "Last"  -> Take[ #, -2 ]
    |>&
]

Output

enter image description here

$\endgroup$
3
  • 1
    $\begingroup$ @rehermans: Thanks very much for your effort. This is what I wanted to have. $\endgroup$ Jan 31 at 15:40
  • $\begingroup$ @TugrulTemel you get a different output to what I show? or you desire a different output? $\endgroup$
    – rhermans
    Jan 31 at 16:11
  • $\begingroup$ Your answer is perfect and it gives me the exact same output you show in your answer. Maybe, my special actual dataset which has 13 columns create a problem for MMA, and hence MMA prefers to transpose the dataset table for a usable output. I think this is what happens. So, no problem with the question nor your answer. $\endgroup$ Jan 31 at 16:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.