The function calculates the differences between all consecutive values of a vector. For your example vector, the differences are:
1 - 10 = -9
1 - 1 = 0
1 - 1 = 0
.
.
.
3 - 1 = 2
10 - 3 = 7
The argument differences
allows you to specify the order of the differences.
E.g., the command
diff(temp, differences = 2)
[1] 9 0 0 0 0 1 -2 1 0 0 0 0 0 2 5
produces the same result as
diff(diff(temp))
[1] 9 0 0 0 0 1 -2 1 0 0 0 0 0 2 5
Hence, it returns the differences of differences.
The argument lag
allows you to specify the lag.
For example, if lag = 2
, the differences between the third and the first value, between the fourth and the second value, between the fifth and the third value etc. are calculated.
diff(temp, lag = 2)
[1] -9 0 0 0 0 1 0 -1 0 0 0 0 0 2 9