I do not understand what the function diff() in R does. See this example:
temp = c(10,1,1,1,1,1,1,2,1,1,1,1,1,1,1,3,10)
diff(temp)
The above code produces the following output:
[1] -9 0 0 0 0 0 1 -1 0 0 0 0 0 0 2 7
What is the definition of this function?
The function calculates the differences between all consecutive values of a vector. For your example vector, the differences are:
The argument
differencesallows you to specify the order of the differences.E.g., the command
produces the same result as
Hence, it returns the differences of differences.
The argument
lagallows 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.