library(splitstackshape)
df$counter <- getanID(cbind(df$country, cumsum(df$event)))[,.id]
We take advantage of the fact that you already have zeroes and ones in your event column. That makes indexing much easier. I combine the country column with cumsum(df$event)
. When that command is run by itself you can see its effect:
cumsum(df$event)
[1] 0 0 1 1 1 2 2 2 3 3
It will only increase with each 1
value. When combined with the country, we are able to see the increase grouped by country.
From there, we can create an id column. @AnandaMahto's splitstackshape
package has the function getanID
for that.
df
country year event counter
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