13
$\begingroup$

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?

$\endgroup$

2 Answers 2

10
$\begingroup$

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

$\endgroup$
1
  • $\begingroup$ Excellent! Thank you. The second way is similar to my workaround, but by far more clear. $\endgroup$
    – mgamer
    Dec 26, 2014 at 16:07
7
$\begingroup$
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|>}
$\endgroup$
4
  • $\begingroup$ Thank you very much! By the way... I simply have overseen the function MaximalBy which is very convenient here... $\endgroup$
    – mgamer
    Dec 26, 2014 at 8:38
  • $\begingroup$ 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. (???) $\endgroup$
    – mgamer
    Dec 26, 2014 at 9:05
  • 1
    $\begingroup$ @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. $\endgroup$
    – WReach
    Dec 26, 2014 at 15:07
  • $\begingroup$ @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 ;-) $\endgroup$
    – mgamer
    Dec 26, 2014 at 15:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.