Generating a lag/lead variables

A few days ago, my friend asked me is there any function in R to generate lag/lead variables in a data.frame or did similar thing as _n in stata. He would like to use that to clean-up his dataset in R.

In stata help manual: _n contains the number of the current observation.
Here’s an example to illustrate what _n does:

Share this:

Like this:

Coming to R from Stata, I was really disappointed at how difficult it was to include lags/leads in ad-hoc analysis. I’m sure would be of use to a great many R beginners. IMO, I’d like to see something like this in r-core. I’d personally prefer if it were two functions though. One named lag() and another lead() since I find the negative number in the second parameter a little strange.

That said, excellent work. It even works put right into a regression so one can quickly test the relationship between y, x, and a lag of x with lm(y ~ x + shift(x,-1)).

Same as you i also came from stata. My feeling is that stata is more convenient than R in some operation(like cleaning economics data). This is because stata is deigned for those purposes and R serve a more general purpose. I think R can do as good as stata if there’s a package to do those.

Another reason is that stata has only one data.frame. Every command you typed would apply to that data.frame directly, you dont have to worry about anything else. If you only have one data.frame, then stata’s environment is very good for that. However, it will drive you crazy if you want to perform something more complicated(just imagine your data contain several data.frame.)

However, R store everything in object. it may be hard for people without programming background to understand what is going on. It would be a bit more difficult to do simple task, but much easier to do complicated task.

I think it’s fine that the r-core do not have those features, but it would be good if we could have a package that implement similar features in stata. I would like to try that if I have more idea and time in future.

last, if you want to have lag and lead separately, you can create:
lag <- function(x,lag) { shift(x,-lag) }
lead <- function(x,lead) { shift(x, lead) }