1

For a financial model to be coded using the dplyr package in R, I need to refer to lagged and lead values of variables but I cannot seem to get the code for the lag function working as documented.

> library(dplyr)
> x <- seq(100,500,100)
> x
[1] 100 200 300 400 500
> lead(x,1)
[1] 200 300 400 500  NA
> lag(x,1)
Error in lag(x, 1) : unused argument (1)
> 

I was expecting lag(x,1) to result in: NA 100 200 300 400. Any advice?Thanks.

alistaire
  • 42,459
  • 4
  • 77
  • 117
  • It gives me the expected result with `lag`. Which version of `dplyr` you are using? Can you try `dplyr::lag(x, 1)` – akrun Feb 07 '16 at 06:40
  • Works fine for me. Try it as `dplyr::lag(x, 1)`; the function masks `stats::lag`, so your namespaces might be getting confused. Reloading `dplyr` will also usually take care of it, but it's something to be aware of regardless—thus the warning when you load `dplyr`. – alistaire Feb 07 '16 at 06:41

1 Answers1

-1

lag is a bit of a strange function with R. It only works as expected with time-series objects. For instance:

cbind(x = ts(1:10), y = lag(1:10, -1))

gives you:

    x  y
    1 NA
    2  1
    3  2
    4  3
    5  4
    6  5
    7  6
    8  7
    9  8
    10  9
    NA 10

If you want a quick and dirty function to do what you want I'd use this:

lag_n <- function(x, n) {c(rep(NA, n), x)[1:length(x)])}

In your case it gives:

lag_n(seq(100,500,100),1)
[1]  NA 100 200 300 400

Edit: Whoops. This is talking about stats::lag. I get the expected result with dplyr::lag. Though I will leave this here for the future.

ClancyStats
  • 1,219
  • 6
  • 12