Would anyone else advise me, how to create time series with lagged in R. I would the result is the difference with lagged, there is a function Delt() but the result is the percentage change. Please advise how to do that. Thank you



I would calculate the computed result with the successive difference. The output format of Delt() is same as I expected, but the result is the percentage change with lagged. I would calculate the difference with lagged. Is there any function, same parameters as Delt() but calculate the difference change.


closed as off-topic by Bob Jansen Apr 9 '16 at 16:45

  • This question does not appear to be about quantitative finance within the scope defined in the help center.
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    $\begingroup$ did you have a look at the function "lag" ? Furthermore this is at most a question for stackoverflow ... $\endgroup$ – Richard Apr 9 '16 at 16:16
  • $\begingroup$ That, or implement shift or get it from data.table. You can do a few things but I agree it doesn't fit here. On StackOverflow it would be a duplicate, so closing. $\endgroup$ – Bob Jansen Apr 9 '16 at 16:45
  • 1
    $\begingroup$ I'm voting to close this question as off-topic because belongs on StackOverflow where it would be a duplicate $\endgroup$ – Bob Jansen Apr 9 '16 at 16:45

Look at the function "lag" and if you want a lag function that does not depend on some time series structure of the object then you can use this one:


  if (length(shift_by)>1)
    return(sapply(shift_by,shift, x=x))

  if (shift_by > 0 )
  else if (shift_by < 0 )
    out<-c(rep(NA,abs_shift_by), head(x,-abs_shift_by))

I prefer it in the meanwhile. It was taken from here.


Not the answer you're looking for? Browse other questions tagged or ask your own question.