13

I have a dataset which I group an then calculate the maximum of an item. When done I get the desired result, but have no column heads. Here the example association (subset of my data):

tn = {<|"year" -> "2004", "month" -> "01", "different" -> 15028|>, <|"year" -> "2004", "month" -> "02", "different" -> 15105|>, <|"year" -> "2004", "month" -> "03", "different" -> 15976|>, <|"year" -> "2004", "month" -> "04",  "different" -> 14016|>, <|"year" -> "2004", "month" -> "05", "different" -> 14196|>, <|"year" -> "2004", "month" -> "06", "different" -> 14068|>, <|"year" -> "2004", "month" -> "07", "different" -> 15321|>, <|"year" -> "2004", "month" -> "08", "different" -> 15034|>, <|"year" -> "2004", "month" -> "09", "different" -> 14745|>, <|"year" -> "2004", "month" -> "10", "different" -> 15160|>, <|"year" -> "2004", "month" -> "11", "different" -> 15069|>, <|"year" -> "2004", "month" -> "12", "different" -> 15982|>, <|"year" -> "2005", "month" -> "01", "different" -> 14381|>, <|"year" -> "2005", "month" -> "02", "different" -> 14721|>, <|"year" -> "2005", "month" -> "03", "different" -> 15965|>, <|"year" -> "2005", "month" -> "04", "different" -> 15546|>, <|"year" -> "2005", "month" -> "05", "different" -> 15024|>, <|"year" -> "2005", "month" -> "06", "different" -> 14225|>, <|"year" -> "2005", "month" -> "07", "different" -> 14554|>, <|"year" -> "2005", "month" -> "08", "different" -> 16700|>, <|"year" -> "2005", "month" -> "09", "different" -> 14965|>, <|"year" -> "2005", "month" -> "10", "different" -> 16788|>, <|"year" -> "2005", "month" -> "11", "different" -> 15382|>, <|"year" -> "2005", "month" -> "12", "different" -> 16606|>}

I form a dataset with

ds = Dataset @ tn

and make the calculation:

ds[GroupBy[#year &]][All, All, "different"][All, Max]

Everything is fine, but I have no heads over the columns (I would like to have "year" and "max") - I could not see how to achieve this. Any hints?

mgamer
  • 5,593
  • 18
  • 26

2 Answers2

10

Working From The Original Expression

The expression proposed in the question can be simplified slightly:

ds[GroupBy[#year&], Max, "different"]

dataset screenshot

This expression can be adjusted to generate a separate association for each year:

ds[GroupBy[#year&] /* Normal, Max, "different"][All, <|"year"->First, "max"->Last|>]

dataset screenshot

Using GroupBy With Reduction

Alternatively, we could add a reduction function to the GroupBy expression:

ds[GroupBy[#, #year &, MaximalBy[#different &] /* First] &]

dataset screenshot

From this we can extract the desired output:

ds[ GroupBy[#, #year&, MaximalBy[#different&] /* First] &
 ][ Values, <|"year"->"year", "max"->"different"|>
 ]

dataset screenshot

WReach
  • 68,832
  • 4
  • 164
  • 269
7
ds[GroupBy[#year &], MaximalBy[#different &]][
  Catenate][All, <|"year" -> #year, "max" -> #different|> & ]
{<|"year" -> "2004", "max" -> 15982|>, <|"year" -> "2005", 
  "max" -> 16788|>}

Or, if you don't mind "month" key in the output:

ds[GroupBy[#year &], MaximalBy[#different &]][Catenate][
  All, <|#, "max" -> #different|> & ] 
{<|"year" -> "2004", "month" -> "12", "different" -> 15982, 
  "max" -> 15982|>, <|"year" -> "2005", "month" -> "10", 
  "different" -> 16788, "max" -> 16788|>}
alancalvitti
  • 15,143
  • 3
  • 27
  • 92
  • Thank you very much! By the way... I simply have overseen the function MaximalBy which is very convenient here... – mgamer Dec 26 '14 at 08:38
  • Another Question: What Version of Mathematica do you use. I´m on 10.0.2.0 and the last part of your first suggestion does not work ([All, <|"year" -> #year, "max" -> #different|> & ]). I get a different output. (???) – mgamer Dec 26 '14 at 09:05
  • 1
    @mgamer Unfortunately 10.0.2.0 introduced some regressions that interfere with the operation of this code. One of the WRI developers acknowledged the bug and provided a patch here. – WReach Dec 26 '14 at 15:07
  • @Wreach: Thank you for the link. I gave it a try, but it seems that it has other side effects (a faced errors in other notebooks which make heavy use of dataset). I made a workaround for me... now waiting for 10.0.3 ;-) – mgamer Dec 26 '14 at 15:58