The documentation for GroupBy
includes the following cases, among others:
GroupBy[{elem1, elem2, … }, {fs1, fs2, … }]
groups into nested associations using fsi at level i.
GroupBy[{elem1, elem2, … }, spec, red]
applies the function red to reduce lists of values that are generated.
Now, when the spec argument in the second form is a list of functions {f1, …, fn}, IOW, in the case of a n-level grouping, it only makes sense that the red argument be applied at the n-th level of the resulting association, since this is the level where the "lists of values that are generated" end up.
Contrary to this expectation, the red argument always gets applied at the first level, even in the case of multi-level groupings.
For example, the 2-level GroupBy
below fails with Lists of unequal length
errors.
data = BlockRandom[SeedRandom[1]; RandomInteger[{1, 1000}, 50]];
GroupBy[data, {Mod[#, 5] == 0 &, Mod[#, 6] == 0 &}, Total]
Is there some general way to specify the red argument so that it behaves properly in all cases?