3
$\begingroup$

I got this type of data :

initDataS = Dataset@<|"Count" -> <|ID01 -> 41667 "Train",
ID02 -> 23288 "Tail" + 18379 "Train",
ID03 -> 30907 "Tail" + 10760 "Train",
ID04 -> 34058 "Tail" + 7100 "Train" + 509 "Loop",
ID05 -> 36256 "Tail" + 5411 "Train",
ID06 -> 37548 "Tail" + 3700 "Train" + 419 "Loop"|>|>

Formated InitDataS

I would like to efficiently sort it out in this way :

FinalDataS = Dataset@ <|"ID01" -> <|"Train" -> 41667, "Tail" -> 0, "Loop" -> 0|>,
"ID02" -> <|"Train" -> 18379, "Tail" -> 23288, "Loop" -> 0|>,
"ID03" -> <|"Train" -> 10760, "Tail" -> 30907, "Loop" -> 0|>,
"ID04" -> <|"Train" -> 7100, "Tail" -> 34058, "Loop" -> 509|>,
"ID05" -> <|"Train" -> 5411, "Tail" -> 36256, "Loop" -> 0|>,
"ID06" -> <|"Train" -> 3700, "Tail" -> 37548, "Loop" -> 419|> |>

Formated FinalDataS

How could i do this ? Thanks in advance for your help !

$\endgroup$
1
$\begingroup$

Should we wish to force the output columns appear in an explicitly specified order, then we can use this:

columns[ls_][e_] := <| FirstCase[e, n_ # :> # -> n, # -> 0, {0, 1}] & /@ ls |>

initDataS[Values, All, columns[{"Train", "Tail", "Loop"}]]

Dataset screenshot

This also allows us to select only a subset of the available columns:

initDataS[Values, All, columns[{"Loop", "Train"}]]

subset Dataset screenshot

$\endgroup$
1
  • $\begingroup$ Thanks, this help me a lot $\endgroup$
    – MedAB
    Mar 1 '20 at 12:36
4
$\begingroup$
Block[{Missing = (0 &)}, 
 Transpose[Transpose@
   initDataS[1, All, Association[# /. Times | Plus -> List /. {a_, b_String} :> b -> a] &]]]

enter image description here

$\endgroup$
2
  • $\begingroup$ Thanks a lot ! Perfectly working $\endgroup$
    – MedAB
    Feb 28 '20 at 15:52
  • 1
    $\begingroup$ So the Transpose forces the dataset to see that it is missing data and apply the Missing? $\endgroup$
    – MikeY
    Feb 28 '20 at 16:23

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.