title: How to filter rows that satisfy condition based on consecutive sequence?
body: <p>I want to subset rows in a dataframe on the condition that a specific sequence of values happen in one row. For example, if in one of the variables there was a pattern 2 | 4 | 5 one after another row by row, that these three rows would be retained, then the search would continue until it found another set of rows that found another 2 | 4 | 5. Eventually, I want to group_by() every set of 2 | 4 | 5 and take the average of the corresponding values in another column. </p>
  
  <p>I have tried to use various lag commands, but have not been able to get this to work. Ideally, the solution would be using dplyr. </p>
  
  <p>Given the following table</p>
  
  <pre class="lang-r prettyprint-override"><code># A tibble: 24 x 2
     information.content scale_degree
                   &lt;dbl&gt;        &lt;dbl&gt;
   1               4.95             0
   2               2.98             2
   3               2.13             4
   4               2.46             2
   5               2.49             4
   6               1.75             5
   7               1.95             7
   8               2.54             5
   9               0.969            4
  10               1.45             2
  11               2.10             4
  12               2.37             2
  13               2.07             4
  14               1.29             5
  15               1.43             7
  16               4.05             7
  17               2.05             5
  18               0.740            4
  19               1.07             2
  20               1.92             0
  21               2.54             2
  22               1.72             4
  23               1.69             2
  24               1.85             0
  
  </code></pre>
  
  <pre class="lang-r prettyprint-override"><code>structure(list(information.content = c(4.951615, 2.9770234, 2.1338997, 
  2.4575028, 2.4881902, 1.7465432, 1.9471669, 2.5410578, 0.96890986, 
  1.4460193, 2.0989947, 2.3660812, 2.0733728, 1.2867087, 1.4285704, 
  4.048128, 2.0472896, 0.74006027, 1.0694636, 1.9193107, 2.5353878, 
  1.721423, 1.6873852, 1.8456767), scale_degree = c(0, 2, 4, 2, 
  4, 5, 7, 5, 4, 2, 4, 2, 4, 5, 7, 7, 5, 4, 2, 0, 2, 4, 2, 0)), row.names = c(NA, 
  -24L), class = c("tbl_df", "tbl", "data.frame"))
  </code></pre>
  
  <p>I would like the following result (would know how to group by and average after no problem) </p>
  
  <pre class="lang-r prettyprint-override"><code># A tibble: 6 x 3
    information.content scale_degree instance
                  &lt;dbl&gt;        &lt;dbl&gt;    &lt;dbl&gt;
  1                2.46            2        1
  2                2.49            4        1
  3                1.75            5        1
  4                2.37            2        2
  5                2.07            4        2
  6                1.29            5        2
  
  </code></pre>
  
  <pre class="lang-r prettyprint-override"><code>structure(list(information.content = c(2.4575028, 2.4881902, 
  1.7465432, 2.3660812, 2.0733728, 1.2867087), scale_degree = c(2, 
  4, 5, 2, 4, 5), instance = c(1, 1, 1, 2, 2, 2)), class = c("spec_tbl_df", 
  "tbl_df", "tbl", "data.frame"), row.names = c(NA, -6L), spec = structure(list(
      cols = list(information.content = structure(list(), class = c("collector_double", 
      "collector")), scale_degree = structure(list(), class = c("collector_double", 
      "collector")), instance = structure(list(), class = c("collector_double", 
      "collector"))), default = structure(list(), class = c("collector_guess", 
      "collector")), skip = 1), class = "col_spec"))
  </code></pre>