How to merge dataset rows conditionally?

dr = DateObject /@ DateRange[{2020, 1, 1}, DatePlus[{2020, 1, 1}, Quantity[15, "Days"]]];

SeedRandom[1]
data = Transpose[{RandomChoice[dr, 30], 
   Round[ RandomReal[10, 30], .1], 
   RandomChoice[{"app", "graphics", "research"}, 30]}]; 

ds = Dataset[AssociationThread[{"Date", "Hours", "Task"}, #] & /@ data];

enter image description here

ds[GroupBy["Date"], All, {"Hours", "Task"}]

enter image description here

ds[GroupBy["Date"], All, "Hours"][All, Total]

enter image description here

Alternatively, you can use GroupBy on data:

GroupBy[data, First -> Rest, 
     Apply[{Total @ #, DeleteDuplicates @ #2} &] @* Transpose] // Dataset

enter image description here

GroupBy[data, First -> (#[[2]] &), Total] // Dataset

enter image description here


While kglr's approach is correct, there is a more succinct one:

ds[GroupBy["Date"],Total,"Hours"]

The reason it works is because

ds[GroupBy["Date"],Total]

produces the total for every column in the grouped result, and "Hours" selects just the one we need.

Tags:

Dataset

Data