title: Removing rows in R where the contiguous values of rows in one column are further than 1 numeric value apart, whilst accounting for participant ID?
body: <p>I am trying to clean up a timeseries with multiple data points. The data is arranged by day and by 'beep'. I want to only keep items that are 1 beep away from each other on the same day.</p>
  <p>In order to do this I have created a dummy variable by multiplying day number by 10 and adding the beep number to it.</p>
  <p>I was wondering if it would be possible to use some kind of clause to specify that I want to keep data that is contiguously = 1 to its lead OR lag variable, but also less than 50 (so that it will keep the days isolated). Alternatively is there a way to group by participant and then by day so that it will apply across participant and across each day in a way that won't delete incorrect data between days e.g. it should not delete day 2 beep 1 for being too far away from day 1 beep 7.</p>
  <p>I am doing this so I can use a function called lagvar from an ESM package to created a time-lagged series. Before doing this I want to make sure that any variables in day_beep that are greater than 1 from their contiguous neighbours are removed.</p>
  <p>E.g.
  Take the following rows and day_beep values</p>
  <pre><code>Participant ID              Day        Beep        Dummy Variable 
      1                          1         1             101
      1                          1         2             102
      1                          1         4             104   
    **1                          1         7             107**
      1                          2         3             203
      1                          2         4             204
      2                          1         2             102
      2                          1         3             103
    **2                          2         5             205
      2                          3         4             305**
    **3                          1         1             101**
      3                          2         4             204
      3                          2         5             205
    **4                          1         7             107**
      4                          4         4             404
      4                          4         5             405
  </code></pre>
  <p>In this instance I would want to remove the data held between the asterisks as it is either contiguously more than 1 beep from its neighbours, or an isolated beep in the series.</p>
  <p>What would be the easiest way to do this for the entire dataframe?</p>
  <p>Any help would be greatly appreciated!</p>