I use a function to do something similar to a SQL sum. I've asked about simplifying it in another post. I'm still using this function and it works well for aggregates like Total
or Mean
. It doesn't work for Count
though. The function is:
mySum[x_, keys1_, keys2_, values_] := Module[{a1, a2, a3, a4, a5},
a1 = Dataset[x];
a2 = a1[GroupBy[keys1], Total, values];
a3 = Normal[Values[a2]];
a4 = Normal[Keys[a2]];
a5 = AssociationThread[keys2 -> #] & /@ a4;
MapThread[Join, {a5, a3}]
];
I tried Count
directly in a Dataset
, where it also failed. I really don't know what to try next. The Dataset
example in Help gets into other sorts of details and I couldn't see anything applicable to this situation.
The only other thing I could think of, which I haven't tried, is using Total
and Mean
, joining the results and calculating the count. That seems pretty awkward.
Does anyone have any suggestions?
a2
from your other question, does the following modification of WReach's answer give what you need:a2[GroupBy[KeyTake[{"L1", "L2"}] -> KeyTake[{"V1", "V2"}]] /* KeyValueMap[Join], Merge[Total@*Counts]]
? $\endgroup$Length
, orTotal@*Unitize
orTotal@*Counts
instead ofCount
? $\endgroup$