I have a data frame, see below. How to compress / delete lines with some conditions? I would like to choose the highest volume only where there is continuous 1in V1
For example: V1of df[2:5,]anyway 1, the highest volume of them is df[4,], so delete df[c(2,3,5),], for df[9:10,], the highest volume of these two is df[10,], delete df[9,]..... so on .... and the result will look like Example image where it V1will look like 0101010101 ....
How to achieve this without the use of loops, how can I achieve this in a vectorized way so that the calculation speed is faster (when working with millions of lines)?
Update:
I apply the same loop with V2, but delete only when Volume is less, so line 13 was deleted in the example, because the volume of line 13 is less than the volume of line 14.
Update (another question):
I tried the akrun method, where I select the maximum Volumefrom the group, but when there are two or more maximums, it will still be continuous 1, as shown below row 9. How to remove duplicate row? I could use duplicated(df$Volume), is there any other way? Thanks
Update:
A subsequent attempt by akrun, the code will delete the lines where V1==0, which is not what I need, I would like to delete only when the V1==1volume is less than the maximum size of this group. I know that this will have some continuous 0s in V1, and deleting duplicated 0s V1will depend on Weight, so I need to delete only V1==1and not deleteV1==0
# Volume Weight V1 V2
#1 0.5367 0.5367 0 1
#4 1.1457 1.1413 1 0
#6 0.5694 0.5633 0 1
#7 1.2368 1.2343 1 0
#8 0.9662 0.9593 0 1
#9 1.4102 1.3923 1 0
#10 1.4102 1.3995 1 0
#11 1.1132 1.1069 0 1
#12 1.4535 1.3923 1 0
#14 1.1475 1.1447 0 1
#15 1.1790 1.1748 1 0
#18 1.1557 1.1552 0 1
Df data frame
Volume Weight V1 V2
1: 0.5367 0.5367 0 1
2: 0.8645 0.8508 1 0
3: 0.8590 0.8585 1 0
4: 1.1457 1.1413 1 0
5: 0.8573 0.8568 1 0
6: 0.5694 0.5633 0 1
7: 1.2368 1.2343 1 0
8: 0.9662 0.9593 0 1
9: 1.3550 1.3412 1 0
10: 1.4102 1.3995 1 0
11: 1.1132 1.1069 0 1
12: 1.4535 1.3923 1 0
13: 1.0437 1.0344 0 1
14: 1.1475 1.1447 0 1
15: 1.1790 1.1748 1 0
16: 1.1749 1.1735 1 0
17: 1.1749 1.1731 1 0
18: 1.1557 1.1552 0 1
Image example
Volume Weight V1 V2
1: 0.5367 0.5367 0 1
4: 1.1457 1.1413 1 0
6: 0.5694 0.5633 0 1
7: 1.2368 1.2343 1 0
8: 0.9662 0.9593 0 1
10: 1.4102 1.3995 1 0
11: 1.1132 1.1069 0 1
12: 1.4535 1.3923 1 0
14: 1.1475 1.1447 0 1
15: 1.1790 1.1748 1 0
18: 1.1557 1.1552 0 1