diff() calculates the difference between values in a vector at a specified lag.
Is there an equivalent function that works on two vectors? For example, I have:
v1 = c(1, 2, 3, 4, 5, 3)
v2 = c(5, 4, 3, 2, 1, 0)
I need to calculate the difference between each value of v1 and v2 at lag 1. That would be:
(2 - 5), (3 - 4), (4 - 3)...
This can be achieved using combinations of head()/tails() on the 2 vectors, but I was wondering if there is already a function that can do the same.
There’s no base function I know of to do this but as gsk3 pointed out the taRifx package has this capability. I would advise against calling a package to do something this simple: You could do:
Or write your own function for storage in .Rprofile