title: Find start and end positions/indices of runs/consecutive values
body: <p>Problem: Given an atomic vector, find the start and end indices of runs in the vector.</p>
  
  <p>Example vector with runs:</p>
  
  <pre><code>x = rev(rep(6:10, 1:5))
  # [1] 10 10 10 10 10  9  9  9  9  8  8  8  7  7  6
  </code></pre>
  
  <p>Output from <code>rle()</code>:</p>
  
  <pre><code>rle(x)
  # Run Length Encoding
  #  lengths: int [1:5] 5 4 3 2 1
  #  values : int [1:5] 10 9 8 7 6
  </code></pre>
  
  <p>Desired output:</p>
  
  <pre><code>#   start end
  # 1     1   5
  # 2     6   9
  # 3    10  12
  # 4    13  14
  # 5    15  15
  </code></pre>
  
  <p>The base <code>rle</code> class doesn't appear to provide this functionality, but the class <a href="http://web.mit.edu/~r/current/arch/i386_linux26/lib/R/library/S4Vectors/html/Rle-class.html" rel="noreferrer"><code>Rle</code></a> and function <a href="https://www.rdocumentation.org/packages/accelerometry/versions/2.2.5/topics/rle2" rel="noreferrer"><code>rle2</code></a> do. However, given how minor the functionality is, sticking to base R seems more sensible than installing and loading additional packages.</p>
  
  <p>There are examples of code snippets (<a href="https://books.google.com/books?id=grfuq1twFe4C&amp;pg=PA32&amp;lpg=PA32&amp;dq=rle%20starts%20ends%20r&amp;source=bl&amp;ots=ms2GT5ONRB&amp;sig=8c524ncWroPQrCQonrpkoghp_wc&amp;hl=en&amp;sa=X&amp;ved=0ahUKEwiEqM__j-PTAhVKwVQKHVNlAcEQ6AEITTAH#v=onepage&amp;q=rle%20starts%20ends%20r&amp;f=false" rel="noreferrer">here</a>, <a href="http://masterr.org/r/how-to-find-consecutive-repeats-in-r/" rel="noreferrer">here</a> and <a href="https://stackoverflow.com/questions/41759473/locate-start-and-end-of-consecutive-values-below-threshold">on SO</a>) which solve the slightly different problem of finding start and end indices for runs which satisfy some condition. I wanted something that would be more general, could be performed in one line, and didn't involve the assignment of temporary variables or values.</p>
  
  <p>Answering my own question because I was frustrated by the lack of search results. I hope this helps somebody!</p>