Backward difference and central difference in R

451 Views Asked by At

I know that in R diff(x, lag = L, differences = K) gives you the lag-L K-th order forward difference. But it only allows for positive lags, so only can give you the forward differences, not backward differences or central differences...

Is there an equivalent function to calculate the lag-L K-th order backward difference and central difference as well though (ideally allowing x to be either a vector or a matrix)? I searched CRAN and can't seem to find anything, but it would seem such a trivial thing that the function ought to exist already somewhere. Anybody any thoughts? diff.zoo allows for negative lags, so can give you negative differences, and by averaging the result of forward & negative differences one could manually get the central diffference. But that only works on zoo objects, not regular vectors or matrices. Anything that I missed? Or should I make my own function based on diff.zoo?

0

There are 0 best solutions below