title: Using R for loop to update column values in data frame
body: <p>I have a simple dataframe with two vectors, "RECORDS" and "FLAG" which looks like this: </p>
  
  <pre><code>RECORDS  FLAG
  H12434   TRUE
  W3211    FALSE
  Maa      FALSE
  Mab      FALSE
  Mac      FALSE 
  Mad      FALSE
  T1_12    FALSE
  H7367    TRUE
  R001     FALSE
  W4810.5  FALSE
  Maa      FALSE
  Mab      FALSE
  T2_12    FALSE
  </code></pre>
  
  <p>I want to change first TRUE to 1 and all the subsequent FALSE to 1, when second TRUE appears increase counter by 1 and change second TRUE and subsequent FALSE to 2. So result should look like this:</p>
  
  <pre><code>RECORDS  FLAG
  H12434   1
  W3211    1
  Maa      1
  Mab      1
  Mac      1
  Mad      1
  T1_12    1
  H7367    2
  R001     2
  W4810.5  2
  Maa      2
  Mab      2
  T2_12    2
  </code></pre>
  
  <p>I tried so many for loops like below but i don't have enough experience to get it work.</p>
  
  <pre><code>counter = 0
  for (i in seq_along(data))
    {
      if(data$flag == TRUE) 
      {
        counter &lt;- counter + 1
        data$flag &lt;- counter
      }
      else
      {
        data$flag &lt;- counter
      }
    }
  </code></pre>
  
  <p>I was hoping someone can help me understand all the things that i am doing wrong here. Thanks.</p>