title: Count changes to contents of a character vector
body: <p>I have a <code>data_frame</code> where a character variable <code>x</code> changes in time. I want to count the number of times it changes, and fill a new vector with this count.</p>
  
  <pre><code>df &lt;- data_frame(
    x = c("a", "a", "b", "b", "c", "b"),
    wanted = c(1, 1, 2, 2, 3, 4)
  )
    x wanted
  1 a      1
  2 a      1
  3 b      2
  4 b      2
  5 c      3
  6 b      4
  </code></pre>
  
  <p>This is similar to, but different from <code>rle(df$x)</code>, which would return </p>
  
  <pre><code>Run Length Encoding
    lengths: int [1:4] 2 2 1 1
    values : chr [1:4] "a" "b" "c" "b"
  </code></pre>
  
  <p>I could try to <code>rep()</code> that output. I have also tried this, which is awfully close, but not for reasons I can't figure out immediately:</p>
  
  <pre><code> df %&gt;% mutate( 
     try_1 = cumsum(ifelse(x == lead(x) | is.na(lead(x)), 1, 0)) 
     )
  Source: local data frame [6 x 3]
  
    x wanted try_1
  1 a      1     1
  2 a      1     1
  3 b      2     2
  4 b      2     2
  5 c      3     2
  6 b      4     3
  </code></pre>
  
  <p>It seems like there should be a function that does this directly, that I just haven't found in my experience.</p>