3
$\begingroup$

I have some date values like

date=Uncompress[FromCharacterCode[
  Flatten[ImageData[Import["http://i.stack.imgur.com/QLINL.png"],"Byte"]]]]

<|08:00:10->5,08:00:14->1,08:00:17->5,08:00:23->1,08:00:24->1,08:00:25->5,08:00:31->1,08:00:32->6,08:00:36->2,...|>

Of course,the hour not always is 08 in my real case.I have a good method to merger the value for every one minute

GroupBy[Normal[date], StringDrop[Keys[#], -3] &, Total[<|#|>] &]

<|08:00->48,08:01->54,08:02->58,08:03->73,08:04->69,08:05->76,08:06->66,08:07->70,08:08->72,08:09->70,08:10->82,08:11->77,08:12->88,08:13->78,08:14->19|>

But if I want to merger the value for every three minutes,is there any concise method to do this?

$\endgroup$
  • $\begingroup$ You could use TimeSeries but you have to define what does it mean to merge every three minutes. $\endgroup$ – Kuba Jun 18 '17 at 9:08
  • $\begingroup$ @Kuba Such as 03:00 and 03:02 should regard one time.In my case,I get some 08:00,08:03,08:06,08:09,08:12,08:14.. $\endgroup$ – yode Jun 18 '17 at 9:10
  • $\begingroup$ Where should the 3minutes window start if the first value date is e.g: 08:04:10. $\endgroup$ – Kuba Jun 18 '17 at 9:18
  • $\begingroup$ @Kuba Like the function BinLists,we should give a sort behavoir firstly.. $\endgroup$ – yode Jun 18 '17 at 9:23
6
$\begingroup$

Try this and let me know:

TimeSeriesAggregate[
   EventSeries @ date, {Quantity[3, "Minutes"], Left}, Total
] 

% // Normal

enter image description here

As you can see it starts with the first date in opposition to rounded example you gave. Don't know how to specify the window start automatically except of adding artificial date at the beginning.

You may also be interested in HistogramList for DateHistogram?

| improve this answer | |
$\endgroup$
2
$\begingroup$

This is probably not the most efficient, but it's convenient and kinda concise:

In[72]:= times = Normal@KeyMap[TimeObject, date];
Map[First@First@# -> Total@Map[Last, #] &,
 Block[{time = First@First@times},
  SplitBy[times,
   If[First[#] - time > Quantity[3, "Minutes"],
     time = First[#];
     True,
     False
     ] &
   ]
  ]
 ]

Out[73]= {TimeObject[{8, 0, 10.}, TimeZone -> -4.] -> 178, 
 TimeObject[{8, 3, 12.}, TimeZone -> -4.] -> 211, 
 TimeObject[{8, 6, 14.}, TimeZone -> -4.] -> 217, 
 TimeObject[{8, 9, 17.}, TimeZone -> -4.] -> 237, 
 TimeObject[{8, 12, 18.}, TimeZone -> -4.] -> 157}
| improve this answer | |
$\endgroup$
  • $\begingroup$ Since every three minutes,it should give some result of 08:00,08:03,08:06,08:09,08:12,08:14. :) $\endgroup$ – yode Jun 18 '17 at 9:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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