This is a follow-up to: R: Subsetting on increasing value to max excluding the decreasing
The posted solution works and now I would like to add a low cutoff based on a second variable. Thus far I'm not certain about how to approach this with data.table. As an example, I would like to restrict output to max of B and all values after the first instance of D == 1 by TrialNum. I assume this means extracting and using the index (using which?) associated with the low cutoff of D.
TrialNum,Obs ,A,B,C,D
1,1,23,1,23,1
1,2,21,2,21,2
1,3,14,3,14,1
1,4,34,4,34,3
1,5,32,5,32,2
1,6,21,3,21,1
1,7,16,5,16,3
1,8,18,2,18,1
2,1,26,1,26,1
2,2,11,2,11,2
2,3,23,3,23,1
2,4,12,4,12,1
2,5,3,2,3,1
2,6,4,3,4,3
2,7,22,1,22,1
2,8,15,2,15,1
Expected output,
TrialNum,Obs,A,B,C,D
1,2,21,2,21,2
1,3,14,3,14,1
1,4,34,4,34,3
1,5,32,5,32,2
2,2,11,2,11,2
2,3,23,3,23,1
2,4,12,4,12,1
So, it's just the first instance of the low cutoff. I don't which to lose data where D drops below threshold after identifying the starting point. Like the solution posted yesterday, I've tried variations of using which in the expression to capture both max(B) and the low cutoff associated with D.
A data.table solution is preferable because it seems currently data.table and dplyr are incompatible on Windows R3.2.0.