title: Create sequential counter that restarts on a condition within panel data groups
body: <p>I have a panel data set for which I would like to create a counter that increases with each step in the panel but restarts whenever some condition occurs. In my case, I'm using country-year data and want to count the passage of years between an event. Here's a toy data set with the key features of my real one:</p>
  
  <pre><code>df &lt;- data.frame(country = rep(c("A","B"), each=5), year=rep(2000:2004, times=2), event=c(0,0,1,0,0,1,0,0,1,0), stringsAsFactors=FALSE)
  </code></pre>
  
  <p>What I'm looking to do is to create a counter that is keyed to <code>df$event</code> within each country's series of observations. The clock starts at 1 when we start observing each country; it increases by 1 with the passage of each year; and it restarts at 1 whenever <code>df$event==1</code>. The desired output is this:</p>
  
  <pre><code>   country year event clock
  1        A 2000     0     1
  2        A 2001     0     2
  3        A 2002     1     1
  4        A 2003     0     2
  5        A 2004     0     3
  6        B 2000     1     1
  7        B 2001     0     2
  8        B 2002     0     3
  9        B 2003     1     1
  10       B 2004     0     2
  </code></pre>
  
  <p>I have tried using <code>getanID</code> from <code>splitstackshape</code> and a few variations of <code>if</code> and <code>ifelse</code> but have failed so far to get the desired result.</p>
  
  <p>I'm already using <code>dplyr</code> in the scripts where I need to do this, so I would prefer a solution that uses it or base R, but I would be grateful for anything that works. My data sets are not massive, so speed is not critical, but efficiency is always a plus.</p>