title: R data.table: How to "label" consecutive values in a column?
body: <p>I have the following data.table (though it's ok if you use it as a data.frame)</p>
  
  <pre><code>library(data.table)
  
  dt &lt;- data.table(first_column = c("item1", "item2", "item3", "item4", "item5", "item6", "item7"),
  second_column = c("cat1", "cat1", "cat1", "cat2", "cat2", "cat2", "cat2"), third_column = c(50, 10, 18, 3092, 731, 189, 1991))
  
  &gt; dt
     first_column second_column third_column
  1:        item1          cat1           50
  2:        item2          cat1           10
  3:        item3          cat1           18
  4:        item4          cat2         3092
  5:        item5          cat2          731
  6:        item6          cat2          189
  7:        item7          cat2         1991
  </code></pre>
  
  <p>I would like to:</p>
  
  <p>(1) create a column which is 1 if the value is &lt;= 1000</p>
  
  <p>(2) then number these unique groupings of 1's</p>
  
  <p>The resulting data.table would look like this:</p>
  
  <pre><code>&gt; dt
  
    first_column second_column  third_column  labels
  0        item1          cat1            50     1
  1        item2          cat1            10     1
  2        item3          cat1            18     1
  3        item4          cat2          3092     0
  4        item5          cat2           731     2
  5        item6          cat2           189     2
  6        item7          cat2          1991     0
  </code></pre>
  
  <p>This would create a column of all zeros and ones:</p>
  
  <pre><code>dt$new[which(dt$third_column &lt; 1000)] = 1
  </code></pre>
  
  <p>How would I then label these "groupings" of 1s? </p>