Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I have a long list like:

list={{"2014 03 12 08 59 34", 0.3849}, {"2014 03 12 08 59 36", 0.38492}, 
      {"2014 03 12 08 59 38", 0.38495}, {"2014 03 12 09 16 20", 1.38522}, 
      {"2014 03 12 09 16 22", 1.38523}, {"2014 03 12 09 16 24", 1.38524},
      {"2014 03 12 09 16 26", 1.38524}, {"2014 03 12 09 16 28", 1.38524},
      {"2014 03 12 09 16 30", 1.38525}, {"2014 03 12 09 16 32", 1.38525}, 
      {"2014 03 12 09 16 34", 1.38525}, {"2014 03 12 10 10 01", 1.38529}, 
      {"2014 03 12 10 10 03", 1.38528}, {"2014 03 12 10 10 05", 1.38531},
      {"2014 03 12 10 10 07", 1.38532}, {"2014 03 12 10 10 09", 1.38534},
      {"2014 03 12 10 10 11", 1.38537}}

The first column is dates and the second some values. The list is sorted by date. I want to gather the elements in a way that every two consecutive elements have time difference less than 4 seconds. The solution should be like:

{{{"2014 03 12 08 59 34", 0.3849}, {"2014 03 12 08 59 36", 0.38492}, 
  {"2014 03 12 08 59 38", 0.38495}}, {{"2014 03 12 09 16 20", 1.38522}, 
  {"2014 03 12 09 16 22", 1.38523}, {"2014 03 12 09 16 24", 1.38524},
  {"2014 03 12 09 16 26", 1.38524}, {"2014 03 12 09 16 28", 1.38524},
  {"2014 03 12 09 16 30", 1.38525}, {"2014 03 12 09 16 32", 1.38525}, 
  {"2014 03 12 09 16 34", 1.38525}}, {{"2014 03 12 10 10 01", 1.38529}, 
  {"2014 03 12 10 10 03", 1.38528}, {"2014 03 12 10 10 05", 1.38531},
  {"2014 03 12 10 10 07", 1.38532}, {"2014 03 12 10 10 09", 1.38534},
  {"2014 03 12 10 10 11", 1.38537}}}

What I have done is I defined

date@da_ :=  DateList[{da, {"Year", " ", "Month", " ", "Day", " ", "Hour", " ", 
                            "Minute", " ", "Second"}}]

Then

ReplaceList[list, {___, {a_, b_}, ___, {c_,     d_}, ___} :> {{a, b}, {c, d}} /;
            Abs@DateDifference[date@a, date@c, "Second"][[1]] < 4]

However this does not match the preferred output.

So how may I cluster my data?

share|improve this question

1 Answer 1

up vote 3 down vote accepted

This converts the special time format to seconds:

toSeconds = 
 AbsoluteTime[{#, {"Year", " ", "Month", " ", "Day", " ", "Hour", " ", "Minute", " ", "Second"}}] &

Then we use Split, taking advantage of the fact that the list is already sorted:

Split[list, toSeconds@First[#2] - toSeconds@First[#1] < 4 &]
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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