Difference between neighbouring elements of a vector
Sounds like diff function
diff(x)
You can also use this code:
x[-1] - x[-length(x)]
x[-1]
- vector x without first element
x[-length(x)]
- vector x without last element
x <- c(1,3,3,9)
(z <- x[-1] - head(x, -1))
# [1] 2 0 6