title: Split and enumerate sequence elements in R based on previous and next values
body: <p>This problem is getting me insane. Say I have this sequence in a boolean vector:</p>
  
  <pre><code>switch &lt;- c(TRUE, TRUE, FALSE, TRUE, rep(FALSE, 6), TRUE, rep(FALSE, 7), TRUE, TRUE, TRUE, FALSE, TRUE)
  </code></pre>
  
  <p>And <code>threshold = 5</code></p>
  
  <p>I basically need to group all TRUE values falling into an interval less or equal the threshold (ahead and behind) with an unique setID. When a TRUE value is met, the counter is reset and this allow the set to expand. In other words, I need this output:</p>
  
  <pre><code>row | VALUE | SET
  ----+-------+-------
  1   | TRUE  | set_1
  2   | TRUE  | set_1
  3   | FALSE | 
  4   | TRUE  | set_1
  5   | FALSE | 
  6   | FALSE | 
  7   | FALSE | 
  8   | FALSE | 
  9   | FALSE | 
  10  | FALSE | 
  11  | TRUE  | set_2 # A new set is created since the last TRUE value was found in the fourth row and 4 + 'threshold' &lt; 11
  12  | FALSE | 
  13  | FALSE | 
  14  | FALSE | 
  15  | FALSE | 
  16  | FALSE | 
  17  | FALSE | 
  18  | FALSE |
  19  | TRUE  | set_3
  20  | TRUE  | set_3
  21  | FALSE | 
  22  | TRUE  | set_3 # Any TRUE value until 22 + 'threshold' = 27 would be put into the set_3
  </code></pre>
  
  <p>Although this looks trivial at first, I was not able to find an efficient way to do it in R.</p>