Missing[]
is generated when an association does not contain the specified key.
data = {<|"a" -> 1, "b" -> 2|>, <|"b" -> 3, "c" -> 4|>} // Dataset
This situation occurs when using Counts
where there are zero instances of a value in some records, as illustrated in this result of a query that I won't bother copying here:
This is Not to be confused with counting missing values, eg, the 263 Missing[]
"age"
values in Titanic data, and the "" keys above).
How to insert key-value pairs representing zero counts to obtain uniform keys in the {Association__}?.
The inserted keys should ideally appear in the same order as they are by the built-in Datset Format functionality (not contained in the Normal form) .
Desired output for data
is:
{<|"a" -> 1, "b" -> 2, "c" -> 0|>, <|"a" -> 0, "b" -> 3,
"c" -> 4|>} // Dataset
EDIT:
It seems KeyUnion
performs exactly this function:
data[KeyUnion] // Normal
(* {<|"a" -> 1, "b" -> 2,
"c" -> Missing["KeyAbsent", "c"]|>, <|"a" ->
Missing["KeyAbsent", "a"], "b" -> 3, "c" -> 4|>} *)
However, it seems immune to ReplaceAll
:
data[KeyUnion, All, Replace[#, Missing[__] :> 0] &] // Normal
(* {<|"a" -> 1, "b" -> 2,
"c" -> Missing["KeyAbsent", "c"]|>, <|"a" ->
Missing["KeyAbsent", "a"], "b" -> 3, "c" -> 4|>} *)
EDIT 2:
Elaborating on WReach's solution using KeyUnion composed with the subquery.
Since the example data is actually structured as key-value pairs, it seems appropriate to extend this question as opposed to asking a new one, though the focus now is on KeyUnion, not the replacement for the Missing
data.
raw2 = <|a -> {"Copy", "Copy", "Copy", "", "", "", "Copy"},
b -> { "Paste", "", "Paste", "Paste", "", "Paste", "Paste"},
c -> {"Copy", "Paste", "Copy", "Copy", "", "", "Paste"},
d -> {"Paste", "", "", "Paste", "", "Paste", "Paste"}|> //
Dataset;
Even though Dimensions are the same as raw
in his answer, and also, raw2[All, Counts] // Normal
gives:
<|a -> <|"Copy" -> 4, "" -> 3|>, b -> <|"Paste" -> 5, "" -> 2|>,
c -> <|"Copy" -> 3, "Paste" -> 2, "" -> 2|>,
d -> <|"Paste" -> 4, "" -> 3|>|>
It's not clear to me at what level to apply KeyUnion
, eg:
raw2[All, KeyUnion, Counts] // Normal
(* <|a -> Missing["Failed"], b -> Missing["Failed"],
c -> Missing["Failed"], d -> Missing["Failed"]|> *)