Let's say we have a set a\of associations:
dataset = { <|"type" -> "a", "subtype" -> "I", "value" -> 1|>, <|"type" -> "a", "subtype" -> "II", "value" -> 2|>, <|"type" -> "b", "subtype" -> "I", "value" -> 1|>, <|"type" -> "b", "subtype" -> "II", "value" -> 2|> } where every entry is unique in terms of {#type, #subtype},
I'd like to build a nested association for more handy querying, e.g. I would like to have:
nested["a", "II", "value"] 2
I can start with
GroupBy[dataset, {#type &, #subtype &}] <| "a" -> <| "I" -> {<|"type" -> "a", "subtype" -> "I", "value" -> 1|>}, "II" -> {<|"type" -> "a", "subtype" -> "II", "value" -> 2|>}|>, "b" -> <| "I" -> {<|"type" -> "b", "subtype" -> "I", "value" -> 3|>}, "II" -> {<|"type" -> "b", "subtype" -> "II", "value" -> 4|>} |>|>
But nested["a", "I"] points to a list with one association, what is expected but I would like to drop that list.
It seems that the third argument of GroupBy isn't generalized to handle nested grouping...
So basically I would like to have ... "I" -> <|"type" -> "a", ....
What is a generic way to go?
I can do:
nested
GroupBy:GroupBy[dataset, #type &, GroupBy[#, #subtype &, First] &]Maplater:GroupBy[dataset, {#type &, #subtype &}] // Map[First, #, {-3}] &
But the first is not handy in general while the second is ugly (and not general either).
Acceptable outputs are:
<| "a" -> <| "I" -> <|"type" -> "a", "subtype" -> "I", "value" -> 1|>, ... |> or
<| "a" -> <| "I" -> <|"value" -> 1|>, ... |> or
<| "a" -> <| "I" -> 1 , ...|> but the first is the most desired one because we may have more that one ("value") key left.


GroupBy[ dataset, {#type &, #subtype &}, Apply[ Association, #, 1 ] & ]? $\endgroup$GroupBy[dataset, {#type &, #subtype &, #type &, #subtype &}], now you would have to adjust levelspec ofApply, and using negative levels may be problematic because the input may be more complex than a list of 1-lvl associations. $\endgroup$GroupBy[ dataset, {#type, #subtype}& ]which leads to a more controlled levelspec (this is also robust for your mimic extended case)? Maybenested[ {"a", "I" }, "value"]is an option? $\endgroup$nested["a"]ornested[[All, "I"]]. $\endgroup$GroupBy[dataset, {#type &, #subtype &}, Map[First]]not suitable? $\endgroup$