title: Calculating time lag between sequential events after grouping for subsets
body: <p>I am trying to calculate the time between sequential observations for different combinations of my columns. I have attached a sample of my data <a href="https://figshare.com/s/47bd7da8f18d954452b6" rel="nofollow noreferrer">here</a>.</p>
  
  <p>A subset of my data looks like:</p>
  
  <pre><code>head(d1) #visualize the first few lines of the data
  
  date       time   year    km sps      pp datetime          prev  timedif   seque
  &lt;fct&gt;      &lt;fct&gt; &lt;int&gt; &lt;dbl&gt; &lt;fct&gt; &lt;dbl&gt; &lt;chr&gt;            &lt;dbl&gt;    &lt;dbl&gt;   &lt;chr&gt;
  2012/06/09 2:22   2012   110 MICRO     0 2012-06-09 02:22     0   260.    00
  2012/06/19 2:19   2012    80 MICRO     0 2012-06-19 02:19     1  4144     01
  2012/06/19 22:15  2012   110 MICRO     0 2012-06-19 22:15     0   100.    00
  2012/06/21 23:23  2012    80 MUXX      1 2012-06-21 23:23     0 33855     10
  2012/06/24 2:39   2012   110 MICRO     0 2012-06-24 02:39     0   120.    00
  2012/06/29 2:14   2012   110 MICRO     0 2012-06-29 02:14     0    43.7   00
  </code></pre>
  
  <p>Where:</p>
  
  <ul>
  <li><code>pp</code>: which species (<code>sps</code>) are predators (coded as 1) and which are prey (coded as 0)</li>
  <li><code>prev</code>: very next <code>pp</code> after the current observation</li>
  <li><code>timedif</code>: time difference (in seconds?) between the current observation and the next one</li>
  <li><code>seque</code>: this is the sequence order: where the first number is the current <code>pp</code> and the second number is the next <code>pp</code></li>
  </ul>
  
  <p>To generate the <code>datetime</code> column, I did this:</p>
  
  <pre><code>d1$datetime=strftime(paste(d1$date,d1$time),'%Y-%m-%d %H:%M',usetz=FALSE) #converting the date/time into a new format
  </code></pre>
  
  <p>To make the other columns I used the following code:</p>
  
  <pre><code>d1 = d1 %&gt;% 
      ungroup() %&gt;% 
      group_by(km, year) %&gt;% #group by km and year because I don't want time differences calculated between different years or km (i.e., locations)
      arrange(datetime)%&gt;%
      mutate(next = dplyr::lead(pp)) %&gt;% 
      mutate(timedif = lead(as.POSIXct(datetime))-as.numeric(as.POSIXct(datetime)))
  d1 = d1[2:nrow(d1),] %&gt;% mutate(seque = as.factor(paste0(pp,prev)))
  </code></pre>
  
  <p>I can then extract the average (geometric mean) time between sequences:</p>
  
  <pre><code>library(psych)
  geo_avg = d1 %&gt;% group_by(seque) %&gt;% summarise(geometric.mean(timedif))
  
  geo_avg 
  # A tibble: 6 x 2
    seque `geometric.mean(timedif)`
    &lt;chr&gt;           &lt;dbl&gt;
  1 00             58830. #prey followed by a prey
  2 01            147062. #prey followed by a predator
  3 0NA               NA  #prey followed by nothing (end of time series)
  4 10            178361. #predator followed by prey
  5 11              1820. #predator followed by predator
  6 1NA               NA  #predator followed by nothing (end of time series)
  </code></pre>
  
  <p><strong>I have one questions that can be broken down into three parts</strong></p>
  
  <ul>
  <li><p>How can I calculate the time difference between:</p>
  
  <ol>
  <li>individuals of the same <code>sps</code> (for example how long does it take for one <code>MICRO</code> to be followed by the next <code>MICRO</code></li>
  <li>species-specific time for opposite classifications prey-predator (<code>01</code> or <code>10</code>) sequences for each prey (<code>pp</code> = 0) or predator (<code>pp</code> = 1) <code>sps</code> (for example, how long does it take for the prey <code>MICRO</code> to be followed by each other predator (<code>pp</code> = 1).</li>
  <li>species-specific time for same classification (<code>00</code> or <code>11</code>) sequences for each prey (<code>pp</code> = 0) or predator (<code>pp</code> = 1) <code>sps</code> (for example, how long does it take for the prey <code>MICRO</code> to be followed by any other prey (<code>pp</code> = 0), <code>MICRO</code> and otherwise.</li>
  </ol></li>
  </ul>
  
  <p>I would like to be able to do something along these lines:</p>
  
  <pre><code>sps    pp    same_sps     same_class   opposite_class   
  MICRO  0     10 days      5 days       2 days    
  MUXX   1     15 days      20 days      12 days
  etc
  </code></pre>
  
  <p>Just in case, here is the output for <code>dput(d1[1:10,])</code>:</p>
  
  <pre><code>structure(list(
   date = structure(c(11L, 21L, 21L, 23L, 26L, 31L,32L, 37L, 38L, 39L), .Label = c("2012/05/30", "2012/05/31", "2012/06/01", "2015/08/19", "2015/08/20"), class = "factor"), 
   time = structure(c(742L, 739L, 915L, 983L, 759L, 734L, 897L, 769L, 901L, 14L), .Label = c("0:00", "0:01", "0:02", "0:03", "9:58", "9:59"), class = "factor"), 
   year = c(2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L), 
   km = c(110, 80, 110, 80, 110, 110, 110, 110, 110, 110), 
   sps = structure(c(9L, 9L, 9L, 11L, 9L, 9L, 9L, 9L, 9L, 9L), .Label = c("CACA", "ERDO", "FEDO", "LEAM", "LOCA", "MAAM", "MAMO", "MEME", "MICRO", "MUVI", "MUXX", "ONZI", "PRLO", "TAHU", "TAST", "URAM", "VUVU"), class = "factor"), 
   pp = c(0, 0, 0, 1, 0, 0, 0, 0, 0, 0), 
   datetime = c("2012-06-09 02:22", "2012-06-19 02:19", "2012-06-19 22:15", "2012-06-21 23:23"), 
   prev = c(0, 1, 0, 0, 0, 0, 0, 0, 0, 0), 
   timedif = c(259.883333333333, 4144, 100.4, 43.2, 2.2, 453.083333333333), 
   seque = c("00", "01", "00", "10", "00", "00", "00", "00", "00", "00")), class = c("grouped_df", "tbl_df", "tbl", "data.frame"), row.names = c(NA, -10L), 
  groups = structure(list(km = c(80, 110), year = c(2012L, 2012L), .rows = list(c(2L, 4L), c(1L, 3L, 5L, 6L, 7L, 8L, 9L, 10L))), row.names = c(NA, -2L), class = c("tbl_df", "tbl", "data.frame"), .drop = TRUE))
  </code></pre>