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];
ds[GroupBy["Date"], All, {"Hours", "Task"}]
ds[GroupBy["Date"], All, "Hours"][All, Total]
Alternatively, you can use GroupBy
on data
:
GroupBy[data, First -> Rest,
Apply[{Total @ #, DeleteDuplicates @ #2} &] @* Transpose] // Dataset
GroupBy[data, First -> (#[[2]] &), Total] // Dataset
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.