Here's a quick demonstration using a numeric vector with 20 entries:
set.seed(123)
x = rnorm(20)
x_res = c(NA, NA,
sapply(3:20, FUN = function(y)
sign(x[y-1]) * as.numeric(sign(x[y-1]) == sign(x[y-2]))))
DF = data.frame(x, x_res)
DF
x x_res
1 -0.56047565 NA
2 -0.23017749 NA
3 1.55870831 -1
4 0.07050839 0
5 0.12928774 1
6 1.71506499 1
7 0.46091621 1
8 -1.26506123 1
9 -0.68685285 0
10 -0.44566197 -1
11 1.22408180 -1
12 0.35981383 0
13 0.40077145 1
14 0.11068272 1
15 -0.55584113 1
16 1.78691314 0
17 0.49785048 0
18 -1.96661716 1
19 0.70135590 0
20 -0.47279141 0
Here's a wrapper function that might make this a bit more versatile (assuming your lag will always be 2
):
create_lag_column <- function(x){
c(NA, NA,
sapply(3:length(x), FUN =
function(y) sign(x[y-1]) * as.numeric(sign(x[y-1]) == sign(x[y-2])))
)
}