I have a data frame like the following:
dat <- data.frame(Target = c(rep("01", times = 8), rep("02", times = 5),
rep("03", times = 4)),
targ2clicks = c(1, 1, 1, 1, 0, 0 ,0 , 1, 1, 0, 0, 0, 1,
0, 0, 0, 1))
Target targ2clicks
1 01 1
2 01 1
3 01 1
4 01 1
5 01 0
6 01 0
7 01 0
8 01 1
9 02 1
10 02 0
11 02 0
12 02 0
13 02 1
14 03 0
15 03 0
16 03 0
17 03 1
Where the first instance for each Target is 1 in the targ2clicks column, I want to remove all rows from the data frame that have 1 in this column before the first occurrence of 0 for that Target. However, where the first value is 0 for a Target, I want to keep all of the values/rows.
What I want to end up with is:
Target targ2clicks
01 0
01 0
01 0
01 1
02 0
02 0
02 0
02 1
03 0
03 0
03 0
03 1
Where all instances for a Target are 1 with no 0s (not in the example df, but just to consider in any solutions), all rows for that Target should be removed.
I have tried coding this in various different ways with no success! Any help hugely appreciated.