Hello all,
I have found that it seems to be very difficult to find subtotals in a Dataset structure. I am talking about simple Mathematica datasets with named columns, consisting of a list of associations.
To give a simple example, suppose a dataset has a column for “City”, another for “Year”, another for “Month”, another for “Date” (within the month in question) and another for amount of “Rain” (in the day in question).
I want to find the city, year, and month that has brough the most total rain. I agree that it should be simple. We would GroupBy (or GatherBy) {city, year, month} and then subtotal rain within those subsets and find the maximum.
I have tried many different ways, devised by myself using the documentation or suggested by AI coding agents. Things like:
winner = mydataset\[GroupBy\[#\[\[{"A", "B", "C"}\]\] &, Total\[#\[All, "D"\]\] &\] /\* MaximalBy\[Identity\] /\* Keys /\* First\]
or
topGroup = mydataset\[GroupBy\[{#A,#B,#C} &\] /\* MaximalBy\[Total\[#\[All, "D"\]\] &\] /\* First\]
when these approaches do not result in some error message, they identify the wrong case as the winner (which is worse).
The only way I have found to get the right answer is to extract subsets of the dataset by the subgroups (not just group the dataset, but actually extract the subset), total the variable in question in the extracted subset, do that for each subset and then find the maximum. This is slow because of the need to extract each subset to analyze it. In a sense, that is what any solution to this problem will do, but it should be able to do it without actually extracting the subset out of the dataset.
I have found other solutions, but they can be said to be essentially the same. Essentially one gives up on the idea of using purely dataset functions and starts using the true and tried list functionality.
Any suggestions would be much appreciated.
Best,
Otto Linsuain