Sciencoded
Sciencoded

Reputation: 31

How to get the difference between next and previous row in an R time series?

I have a quite large time series comprising around 14k observations of 4 variables (date, x, y, z).

How can I, (contrary to the function diff( df$vector, lag = 1) which computes the difference between the current value (t) and the previous one (t-1)), calculate for each value the difference between the next value (t+1) and the previous value (t-1)?

Upvotes: 3

Views: 3503

Answers (3)

hvollmeier
hvollmeier

Reputation: 2986

What you are looking for is diff of 2 ! (You should use diff not lag as suggested by @Kake_Fisk.) Using the data from the accepted answer from @Coatless:

set.seed(11)
a = sample(1:10, 10)
diff(a,2)

gives:

[1]  2  8  2 -1 -1 -4 -4  6

edit after comment from OP:

If I understand your comment correctly your accepted answer from @coatless does not give you what you were asking for. You are looking for a series which gives you the previous and next differences of each element to it's neighbours. So the length of this series has to be 2 times minus 2 ( as the first element has no previous value and the last one no next value ) of the original series. One possible solution might be:

set.seed(11)
a = sample(1:10, 10)
a
[1]  3  1  5  9  7  8  6  4  2 10

prev <- a[1:(length(a)-1)] - a[2:length(a)] # series of previous values
nxt <- -1 * prev # series of next values

nextPrevValues <- rep(0,2 * (length(a)-1))
for(i in 1:length(a)-1){
  nextPrevValues[2*i-1] <- nxt[i]
  nextPrevValues[2*i] <- prev[i]
}

Starting with the difference from the first element to the next you get the previous and next differences for each element. The last element only has a previous value.

nextPrevValues
 [1] -2  2  4 -4  4 -4 -2  2  1 -1 -2  2 -2  2 -2  2  8 -8

Upvotes: 0

coatless
coatless

Reputation: 20746

So, to understand the request... Generate some data:

set.seed(11)
a = sample(1:10, 10)

Data is given as:

3  1  5  9  7  8  6  4  2 10

Need T+1 vs. T-1:

T = 0 => No computation
T = 1 => 5 - 3 = 2
T = 2 => 9 - 1 = 8
...
T = 9 => 10 - 4 = 6
T = 10 => No computation

With that being established...

#' Future Difference
#' 
#' Obtain the lagged difference between X[t+1+lag] - X[t-1-lag]
#' @param x   A \code{vec}
#' @param lag A \code{integer} indicating the lag
#' @return A \code{vec} with differences taken at T+lag v. T-lag
#' @examples
#' set.seed(11)
#' a = sample(1:10, 12)
#' fdiff(a)
fdiff = function(x, lag = 1){
  # Number of obs
  n = length(x)

  # Trigger error to prevent subset
  if(n < 2+lag){stop("`x` must be greater than `2+lag`")}

  # X_(T+1) - X_(T-1)
  x[(2+lag):n] - x[1:(n-lag-1)]
}

Calling it on a gives:

fdiff(a)

2  8  2 -1 -1 -4 -4  6

Upvotes: 1

Kake_Fisk
Kake_Fisk

Reputation: 1165

If I understand your question correctly, all you want is to set lag = 2.

Upvotes: 0

Related Questions