The diff function in R returns suitably lagged and iterated differences.
x = c(1, 2, 1, 3, 11, 7, 5)
diff(x)
# [1] 1 -1 2 8 -4 -2
diff(x, lag=2)
[1] 0 1 10
For the record, I asked this question to figure out how to register sign changes in a vector of numbers, thanks to @dickoa 's answer, I got it done this way:
require(zoo)
equals = function(x) all(diff(x) == 0)
x = c(2, 3, -1, 3, -2, -5)
y = sign(x)
rollapply(y, 2, equals)
[1] TRUE FALSE FALSE FALSE TRUE