title: Sum if the date difference is smaller than a value
body: <p>I have a database that contains the registers of errors of a series of machines, with their correspondent date. There are several kind of errors. Ie:</p>
  
  <p><a href="https://i.stack.imgur.com/0ZMUM.png" rel="nofollow noreferrer">initial data table</a></p>
  
  <pre><code>          fechayhora        id tipo
  1: 2017-03-21 11:03:00 A2_LR1_Z1  APF
  2: 2017-05-03 10:34:00 A2_LR1_Z1  APF
  3: 2017-05-17 08:52:00 A2_LR1_Z1  APF
  4: 2017-05-17 10:46:00 A2_LR1_Z1  APF
  5: 2017-05-17 14:23:00 A2_LR1_Z1  APF
  6: 2017-05-17 17:29:00 A2_LR1_Z1  APF
  </code></pre>
  
  <p>I would to add a column that contains the sum of the events tipye "APF" that have occured in the previous, lets say 12 hours (a parameter actually that I could vary).</p>
  
  <p>Result expected:</p>
  
  <pre><code>          fechayhora        id tipo    number_of_APF_12h
  1: 2017-03-21 11:03:00 A2_LR1_Z1  APF  0
  2: 2017-05-03 10:34:00 A2_LR1_Z1  APF  0
  3: 2017-05-17 08:52:00 A2_LR1_Z1  APF  0
  4: 2017-05-17 10:46:00 A2_LR1_Z1  APF  1
  5: 2017-05-17 14:23:00 A2_LR1_Z1  APF  2
  6: 2017-05-17 17:29:00 A2_LR1_Z1  APF  3 
  </code></pre>