A brief version of my df is like the following:
A <- c(10,50,50,10,60,70,50,20,60,10)
B <- c(0,3,2,4,1,3,5,1,1,2)
df<- data.frame(A,B)
df
A B
10 0
50 3
50 2
10 4
60 1
70 3
50 5
20 1
60 1
10 2
I want to generate an output C based on some conditions of column A and values in column B.
Conditions:
- C=0 when A < 50 or A "just becomes" >= 50
- C= abs(B[n+1] - B[n]) when both B[n+1] and B[n] are >= 50
Here is an example of what I want in column C:
A B C Explaination
10 0 0 A <50, so C=0
50 3 0 A just becomes >= 50, so C is still 0
50 2 1 We have two consecutive A >= 50, so C = abs(2-3) = 1
10 4 0 A <50, so C=0
60 1 0 A just becomes >= 50, so C is still 0
70 3 2 We have two consecutive A >= 50, so C = abs(3-1) = 2
50 5 2 consecutive rows with A >= 50, so C = abs(5-3) = 2
20 1 0 A <50, so C=0
60 1 0 A just becomes >= 50, so C is still 0
10 2 0 A <50, so C=0