# Slide: one function for lag/lead variables in data frames, including time-series cross-sectional data

**Christopher Gandrud (간드루드 크리스토파)**, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

I often want to quickly create a lag or lead variable in an R data frame. Sometimes I also want to create the lag or lead variable for different groups in a data frame, for example, if I want to lag GDP for each country in a data frame.

I've found the various R methods for doing this hard to remember and usually need to look at old blog posts. Any time we find ourselves using the same series of codes over and over, it’s probably time to put them into a function.

So, I added a new command–`slide`

–to the DataCombine R package (v0.1.5).

Building on the `shift`

function TszKin Julian posted on his blog, `slide`

allows you to slide a variable up by any time unit to create a lead or down to create a lag. It returns the lag/lead variable to a new column in your data frame. It works with both data that has one observed unit and with time-series cross-sectional data.

Note: your data needs to be in ascending time order with equally spaced time increments. For example 1995, 1996, 1997.

## Examples

### Not Cross-sectional data

Let's create an example data set with three variables:

# Create time variable<br />Year <- 1980:1999<br /><br /># Dummy covariates<br />A <- B <- 1:20<br /><br />Data1 <- data.frame(Year, A, B)<br /><br />head(Data1)<br />

## Year A B<br />## 1 1980 1 1<br />## 2 1981 2 2<br />## 3 1982 3 3<br />## 4 1983 4 4<br />## 5 1984 5 5<br />## 6 1985 6 6<br />

Now let's lag the `A`

variable by one time unit.

library(DataCombine)<br /><br />DataSlid1 <- slide(Data1, Var = "A", slideBy = -1)<br /><br />head(DataSlid1)<br />

## Year A B A-1<br />## 1 1980 1 1 NA<br />## 2 1981 2 2 1<br />## 3 1982 3 3 2<br />## 4 1983 4 4 3<br />## 5 1984 5 5 4<br />## 6 1985 6 6 5<br />

The lag variable is automatically given the name `A-1`

.

To lag a variable (i.e. the lag value at a given time is the value of the non-lagged variable at a time in the past) set the `slideBy`

argument as a negative number. Lead variables, are created by using positive numbers in `slideBy`

. Lead variables at a given time have the value of the non-lead variable from some time in the future.

### Time-series Cross-sectional data

Now let's use `slide`

to create a lead variable with time-series cross-sectional data. First create the example data:

# Create time and unit ID variables<br />Year <- rep(1980:1983, 5)<br />ID <- sort(rep(seq(1:5), 4))<br /><br /># Dummy covariates<br />A <- B <- 1:20<br /><br />Data2 <- data.frame(Year, ID, A, B)<br /><br />head(Data2)<br />

## Year ID A B<br />## 1 1980 1 1 1<br />## 2 1981 1 2 2<br />## 3 1982 1 3 3<br />## 4 1983 1 4 4<br />## 5 1980 2 5 5<br />## 6 1981 2 6 6<br />

Now let's create a two time unit lead variable based on `B`

for each unit identified by `ID`

:

DataSlid2 <- slide(Data2, Var = "B", GroupVar = "ID",<br /> slideBy = 2)<br /><br />head(DataSlid2)<br />

## Year ID A B B2<br />## 1 1980 1 1 1 3<br />## 2 1981 1 2 2 4<br />## 3 1982 1 3 3 NA<br />## 4 1983 1 4 4 NA<br />## 5 1980 2 5 5 7<br />## 6 1981 2 6 6 8<br />

Hopefully you'll find `slide`

useful in your own data analysis. Any suggestions for improvement are always welcome.

**leave a comment**for the author, please follow the link and comment on their blog:

**Christopher Gandrud (간드루드 크리스토파)**.

R-bloggers.com offers

**daily e-mail updates**about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.