A method for creating a variable that sequential counts until an binary event occurs in another vairiable.
Create an data set
The dataset consists of and id, an event that is 0 or 1 %20 of the time, and a panel of "a". The panel was created because I could only find a solution that was grouped by a panel.
mutate(event = sample(1:10, size=30, replace=T))%>%
mutate(event=if_else(event>8, 1, 0))%>%
mutate(panel = "a")
This code makes the counter which is reset everytime the event is 1.