Collect data points in a list into weekly groups?
You can GatherBy
using DateValue
with "Week"
({"Year","Week"}
if the input data spans multiple years) as date element as the second argument:
GatherBy[data, DateValue[#[[1]], "Week"] &]
{{{{2002, 7, 30}, 1.20431}, {{2002, 7, 31}, 0.52822}, {{2002, 8, 1}, -1.51937}, {{2002, 8, 2}, -1.2966}},
{{{2002, 8, 5}, -1.97334}, {{2002, 8, 6}, 1.07311}, {{2002, 8, 7}, 0.224605}, {{2002, 8, 8}, 1.78266}, {{2002, 8, 9}, 0.90241}},
{{{2002, 8, 12}, 0.502187}}}
Additionally, CurrentDate
and PreviousDate
work too. CurrentDate
appears to be implemented in terms of PreviousDate
. Of course, NextDate
would also work.
GatherBy[data, CurrentDate[First[#], "Week"] &]
{{{{2002, 7, 30}, 1.20431}, {{2002, 7, 31}, 0.52822}, {{2002, 8, 1}, -1.51937}, {{2002, 8, 2}, -1.2966}}, {{{2002, 8, 5}, -1.97334}, {{2002, 8, 6}, 1.07311}, {{2002, 8, 7}, 0.224605}, {{2002, 8, 8}, 1.78266}, {{2002, 8, 9}, 0.90241}}, {{{2002, 8, 12}, 0.502187}}}
GatherBy[data, PreviousDate[First[#], "Week"] &]