8
$\begingroup$

When processing a larger Dataset I came up do a point where I want to form a dataset with culumn heads from an intermediate structure. Here is an example of this structure:

test = {<|"name" -> "alpha", 
     "group" -> "one"|> -> {<|"value" -> 459|>}, <|"name" -> "beta", 
     "group" -> "two"|> -> {<|"value" -> -338|>}, <|"name" -> "gamma",
      "group" -> "two"|> -> {<|"value" -> 363|>}};

making a dataset:

assoc = Association@test; Dataset@assoc

this gives:

enter image description here

Now I´m searching a way to make this a dataset with column-heads "name", "group" and "value". Maybe I´m blind for a moment, but I could not manage it??? Can anyone give me a hint?

$\endgroup$
4
$\begingroup$
test = {<|"name" -> "alpha", "group" -> "one"|> -> {<|"value" -> 459|>}, 
        <|"name" -> "beta", "group" -> "two"|> -> {<|"value" -> -338|>}, 
        <|"name" -> "gamma", "group" -> "two"|> -> {<|"value" -> 363|>}};

Association /@ Flatten /@ Normal[test /. Rule -> List] // Dataset

Dataset

$\endgroup$
1
  • $\begingroup$ Thanks a lot! I´ve simply overseen the step /.Rule->List...o.l. sometimes one does no see the obvious $\endgroup$ – mgamer Dec 7 '14 at 14:31
7
$\begingroup$

If we assume that we are starting from the exhibited dataset:

dataset = Dataset@assoc;

... then we can reshape it like this:

dataset[All, Apply@Association]

Or, equivalently:

dataset[Map[Apply@Association]]

dataset screenshot

This will also do the trick, although it is a little messy since MapThread presently lacks an operator form:

dataset[{Keys, Values}][MapThread[Association, #]&]

Edit

And here is yet another way:

dataset[All, <| Keys@#, Values@# |> &]
$\endgroup$
5
$\begingroup$
Dataset[Join@@@({#,Join@@#2}&@@@test)]
(* or  Dataset[Join@@@({#,Sequence@@#2}&@@@test)] *)

enter image description here

$\endgroup$
1
  • 1
    $\begingroup$ Must be a record for @@@ density. $\endgroup$ – alancalvitti Jan 8 '15 at 18:11

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.