I want to classify the rows of a data frame based on a threshold applied to a given numeric reference column. If the reference column has a value below the threshold, then the result is 0, which I want to add to a new column. If the reference column value is over the threshold, then the new column will have value 1 in all consecutive rows with value over the threshold until a new 0 result comes up. If a new reference value is over the threshold then the value to add is 2, and so on.
If we set up the threshold > 2 then an example of what I would like to obtain is:
row | reference | result |
---|---|---|
1 | 2 | 0 |
2 | 1 | 0 |
3 | 4 | 1 |
4 | 3 | 1 |
5 | 1 | 0 |
6 | 6 | 2 |
7 | 8 | 2 |
8 | 4 | 2 |
9 | 1 | 0 |
10 | 3 | 3 |
11 | 6 | 3 |
row <- c(1:11)
reference <- c(2,1,4,3,1,6,8,4,1,3,6)
result <- c(0,0,1,1,0,2,2,2,0,3,3)
table <- cbind(row, reference, result)
Thank you!