title: List creation with incremental values triggered by logical vector in R
body: <p>I am trying to create a vector of ordered integers of the same length as a vector of logicals. The list starts at 1 and continues with this value until a <code>TRUE</code> is encountered, which triggers an increase in the next element, and so on until all logicals are parsed.</p>
  
  <p>I've got this working in a <code>for</code> loop, but I need to repeat the process many times over different groups and am wondering if something more efficient is recommended.</p>
  
  <pre><code>dat &lt;- c(TRUE, FALSE, TRUE, TRUE, FALSE,
    TRUE, TRUE, TRUE, TRUE, FALSE,
    TRUE, FALSE, TRUE, FALSE, TRUE,
    FALSE, FALSE, TRUE, TRUE, TRUE, TRUE)
  
  counter &lt;- 1
  output &lt;- c()
  for (i in dat) {
    output &lt;- c(output, counter)
    if(i == T)
      counter &lt;- counter + 1
  }
  
  output
  
  &gt; [1]  1  2  2  3  4  4  5  6  7  8  8  9  9 10 10 11 11 11 12 13 14
  </code></pre>
  
  <p>Thanks.</p>
  
  <h1>Update</h1>
  
  <p>Per Stéphane's comment - <code>c(1, 1+cumsum(dat))[1:length(dat)]</code> works great, thanks!</p>