Re-edited to include additional raw input data.
I have a breathing time series in which the detected accuracy of the intake of breath varies due to a technical limitation of the detector setup. The series contains a list of the detected intervals between breaths.
I have calculated the difference in the breathing rate between breaths by subtraction of the 2nd observation from the 1st, and so on. This interval has then been examined using an ifelse statement to flag a positive value greater than 0.5 as 1, or else 0. For example.
sequence rate diff flag
1 1 1.07 0.00 0
2 2 0.99 0.08 0
3 3 0.98 0.01 0
4 4 1.01 -0.03 0
5 5 1.10 -0.09 0
6 6 0.99 0.11 0
7 7 1.06 -0.07 0
8 8 0.23 0.83 1
9 9 0.82 -0.59 0
10 10 1.09 -0.27 0
11 11 0.96 0.13 0
12 12 1.10 -0.14 0
13 13 1.04 0.06 0
14 14 0.36 0.68 1
15 15 0.75 -0.39 0
Empirically I have determined that most commonly errors result in 2 shorter than normal intervals followed by an interval similar to that of the row which contains the > 0.5 value. For example, row 8, is followed by two shorter intervals and then row 10 is back to the expected rate.
In a spreadsheet I have used this if/and statement to evaluate whether to sum together the extracted row 9 and 10.
IF(AND(row8<row7,row9<row7,sum(row8:row9)<120%*row7,sum(row8:row9),0)
I have a separate checking statement to test whether row 10 has been determine correctly.
IF(AND(row7*80%<=row10,120%*row7*120%>=row10),0,1)
Thus I want to generate a variable that has the 4 extracted rows, has the calculated summed rows(1.11 or 0), and the check row (1 or 0).
seq rate diff sum check
7 7 1.06 -0.07 0 0
8 8 0.23 0.83 1.05 1
9 9 0.82 -0.59 0 0
10 10 1.09 -0.27 0 0
I then need to reinsert the row containing the sum value when the check value = 1, back into the dataframe. I could then resort the data frame on the seq to have the row in the correct sequence.
Finally, I need to loop down the dataframe and process the next set of rows flagged by a 1.
Be stoked if I could get some clues how to achieve this.
regards
Andrew