I have data with 2 columns like this :
v1 = c(0, 29, 32, 29, 32, 28, -59, 30)
v2 = c(0, 0, 0, 0, 1, 1, 0, 1)
data = data.frame(v1, v2)
v1 v2
1 0 0
2 29 0
3 32 0
4 29 0
5 32 1
6 28 1
7 -59 0
8 30 1
I want to change the values of the column v2 as follows: if the value of v1 is negative, then change all previous 1s of v2 to 0. I can do this using applying a for loop using R. Is there a way to do the same thing without applying a for loop (probably using dplyr package)?
Update
My actual problem is more general than the situation I explained earlier. I apologize for the confusion. My data can have more than one negative values like in the given example:
