title: R Conditional Counter based on multiple columns
body: <p>I have a data frame with multiple responses from subjects (subid), which are in the column labelled trials. The trials count up and then start over within one subject.</p>
  
  <p>Here's an example dataframe:</p>
  
  <pre><code>subid &lt;- rep(1:2, c(10,10))
  trial &lt;- rep(1:5, 4)
  response &lt;- rnorm(20, 10, 3)
  
  df &lt;- as.data.frame(cbind(subid,trial, response))
  df
  
     subid trial  response
  1      1     1  3.591832   
  2      1     2  8.980606     
  3      1     3 12.943185     
  4      1     4  9.149388     
  5      1     5 10.192392     
  6      1     1 15.998124     
  7      1     2 13.288248     
  </code></pre>
  
  <p>I want a column that increments every time trial starts over within one subject ID (subid):</p>
  
  <pre><code>df$block &lt;- c(rep(1:2, c(5,5)),rep(1:2, c(5,5)))
  df
       subid trial response block
  1      1     1  3.591832     1
  2      1     2  8.980606     1
  3      1     3 12.943185     1
  4      1     4  9.149388     1
  5      1     5 10.192392     1
  6      1     1 15.998124     2
  7      1     2 13.288248     2
  </code></pre>
  
  <p>The trials are not predictable in where they will start over. My solution so far is messy and utilizes a for loop.</p>
  
  <p>Solution:</p>
  
  <pre><code>block &lt;- 0
  blocklist &lt;- 0
  
  for (i in seq_along(df$trial)){
     if (df$trial[i]==1){
       block = block + 1}else
     if (df$trial!=1){
       block = block}
    blocklist&lt;- c(blocklist, block)
  }
  
  blocklist &lt;- blocklist[-1]
  df$block &lt;- blocklist
  </code></pre>
  
  <p>This solution does not start over at a new subid. Before I came to this I was attempting to use Wickham's tidyverse with mutate() and ifelse() in a pipe. If somebody knows a way to accomplish this with that package I would appreciate it. However, I'll use a solution from any package. I've searched for about a day now and don't think this is a duplicate question to other questions like <a href="https://stackoverflow.com/questions/32592741/conditional-counter-in-new-column-r">this</a>.</p>