3
$\begingroup$

Given the ragged dataset in form of association of associations:

<|
  "r1" -> <|"a" -> 2, "b" -> 3          |>, 
  "r2" -> <|          "b" -> 5, "c" -> 6|>
|>

How do I construct a Dataset with missing keys set to Missing or default value 0?

Like

<|
  "r1" -> <|"a" -> 2, "b" -> 3  "c" -> 0|>, 
  "r2" -> <|"a" -> 0  "b" -> 5, "c" -> 6|>
|>

Of course, the actual dataset is very large and I need it in Dataset object

$\endgroup$

2 Answers 2

5
$\begingroup$

Given:

assoc = <| "r1" -> <|"a" -> 2, "b" -> 3          |>
         , "r2" -> <|          "b" -> 5, "c" -> 6|>
         |>;

... here is a way using KeyUnion:

AssociationThread[assoc // Keys, KeyUnion[assoc // Values, 0&]]

(* <| "r1" -> <|"a" -> 2, "b" -> 3, "c" -> 0|>
    , "r2" -> <|"a" -> 0, "b" -> 5, "c" -> 6|>
    |>
*)

The default value function 0& can be generalized to return a different value based upon the key which is passed as an argument. For example:

default["a"] := 999
default["c"] := -999

AssociationThread[assoc // Keys, KeyUnion[assoc // Values, default]]
(* <| "r1" -> <|"a" -> 2,   "b" -> 3, "c" -> -999|>
    , "r2" -> <|"a" -> 999, "b" -> 5, "c" -> 6   |>
    |>
*)
$\endgroup$
4
$\begingroup$
data = <|
  "r1" -> <|"a" -> 2, "b" -> 3|>,
  "r2" -> <|"b" -> 5, "c" -> 6|>
  |>
keys = Union @@ Map[Keys, data]  (* EDIT: ht Anton *)
f[a_Association] := Association[# -> Lookup[a, #, Missing[]] & /@ keys]
Dataset[f /@ data]
$\endgroup$
4
  • 1
    $\begingroup$ I think keys=Union @@ Map[Keys, data] is better. (+1) $\endgroup$ Jul 20, 2017 at 15:47
  • $\begingroup$ @AntonAntonov I agree, if we are sure that no dataset might be missing all values for some key. $\endgroup$
    – Alan
    Jul 20, 2017 at 15:50
  • $\begingroup$ Did I not say that the dataset is very large (and I can not write all keys by hand)? Accepted with keys=Union @@ Map[Keys, data] $\endgroup$
    – grandrew
    Jul 20, 2017 at 16:26
  • $\begingroup$ @grandrew Well no, you did not say that you do not have separate access to the keys, nor that you can be sure that a given dataset has all they keys you need. But from your comment, you apparently intended to. So I have edited the answer to include Anton's suggestion. $\endgroup$
    – Alan
    Jul 20, 2017 at 18:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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