title: Create group number for contiguous runs of equal values
body: <p>Is there is a faster way to make a counter index than using a loop? For each contiguous run of equal values, the index should be the same. I find the looping very slow especially when the data is so big.</p>
  <p>For illustration, here is the input and desired output</p>
  <pre><code>x &lt;- c(2, 3, 9, 2, 4, 4, 3, 4, 4, 5, 5, 5, 1)
  </code></pre>
  <p>Desired resulting counter:</p>
  <pre><code>c(1, 2, 3, 4, 5, 5, 6, 7, 7, 8, 8, 8, 9)
  </code></pre>
  <p>Note that <em>non</em>-contiguous runs have <em>different</em> indexes. E.g. see the desired indexes of the values <code>2</code> and <code>4</code></p>
  <p>My inefficient code is this:</p>
  <pre><code>group[1]&lt;-1
  counter&lt;-1
  for (i in 2:n){
  if (x[i]==x[i-1]){
      group[i]&lt;-counter
  }else{
      counter&lt;-counter+1
      group[1]&lt;-counter}
  }
  </code></pre>