How to use for-loop to create duration variable?

I need to create a variable that tells me how many years there was peace before the variable conflict == 1 (meaning a conflict starts).

Someone recommended to me to do a for-loop with a variable that starts with 0, adds 1 if conflict==0, saves the value if conflict==1 and puts it back to 0 afterwards. Can someone help me with how I put this into R code language?

Thank you!

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum