title: How do I remove rows based on time, location and individual criteria in R
body: <p>I'm trying to remove redundant data rows from a gigantic dataset. For the same individual, at the same location, on the same day, I want to keep just one detection for every 10 minutes time range. So for example if individual 1 stays around station 6 for 20 minutes, instead of 200 or so detections I just want to keep 2, one for every 10 minutes he's there. Here is an example dataset:</p>
  <pre><code>datetime&lt;-c(&quot;2020-12-30 23:03:24&quot;,&quot;2020-12-30 23:04:25&quot;,&quot;2020-12-28 23:06:20&quot;, &quot;2020-12-26 12:02:10&quot;,&quot;2020-12-26 12:07:26&quot;,&quot;2020-12-26 12:10:07&quot;, &quot;2018-05-11 05:02:05&quot;,&quot;2018-05-11 05:03:07&quot;, &quot;2018-05-11 05:13:25&quot;, &quot;2018-05-11 05:14:27&quot;)
  dt&lt;-as.POSIXct(datetime, format=&quot;%Y-%m-%d %H:%M:%S&quot;)
  i&lt;-c('ind1','ind1','ind1', 'ind2', 'ind2', 'ind2', 'ind1', 'ind1', 'ind1', 'ind4')
  l&lt;-c('station1', 'station1', station1','station2','station2','station3','station1','station1','station2','station6')
  stack&lt;-data.frame(dt, i,l)
  </code></pre>
  <p>The expected resulting dataframe:</p>
  <pre><code>                    dt    i        l
  1  2020-12-30 23:03:24 ind1 station1
  2  2020-12-30 23:04:25 ind1 station1
  3  2020-12-28 23:06:20 ind1 station1
  4  2020-12-26 12:02:10 ind2 station2
  5  2020-12-26 12:07:26 ind2 station2
  6  2020-12-26 12:10:07 ind2 station3
  7  2018-05-11 05:02:05 ind1 station1
  8  2018-05-11 05:03:07 ind1 station1
  9  2018-05-11 05:13:25 ind1 station2
  10 2018-05-11 05:14:27 ind4 station6
  </code></pre>
  <p>Here is what I have tried to code so far:</p>
  <pre><code>#Separate date and time
  stack &lt;- tidyr::separate(stack, dt, c(&quot;date&quot;, &quot;time&quot;), sep = &quot; &quot;)
  
  #Merge columns location (l), individual (i) and date (date)
      data_set_merged &lt;- stack%&gt;%
        unite(&quot;Merged_sample&quot;,  c(&quot;i&quot;, &quot;l&quot;, &quot;date&quot;), remove=FALSE)
  
  #Order dataset chronologically 
  data_set_merged %&gt;% arrange(ymd(data_set_merged$date))
  
  #Count number of minutes between every group of detection
  data_set_merged$time&lt;-as.POSIXct(as.character(data_set_merged$time), format=&quot;%H:%M:%S&quot;)
  value &lt;-diff(data_set_merged$time)
  
  #Add NA value at the end, since no difference between last value and nothing
  Adding_NA_value &lt;- append(value , &quot;NA&quot;)
  
  New_data_frame_with_column&lt;- data_set_merged %&gt;%
    dplyr::mutate (Time_intervall_seconds = Adding_NA_value)
  
  #Group_by sample, site, year and day and select the observations with &gt; 10 min (= less than 600 seconds) replicates
  Final_data_frame &lt;- New_data_frame_with_column %&gt;%
    group_by(Merged_sample)%&gt;%
    filter (Time_intervall_seconds&gt;= 600)
  </code></pre>
  <p>This code deletes more than juste the redundant data: if two rows are less than 600 seconds, it deletes both instead of just one, so I'm losing information. I also don't know if it is only calculating the time difference for each group - I don't want to delete detections between individuals, or between locations for the same individual, I really only want it for the same location + individual + date.
  I tried ordering it chronologically to get a time interval that makes sense, but then the interval is not calculated by group anymore.
  I'm still quite new at R and I don't know where to go from here. Any help would be sososo welcome. Thanks!</p>