**Christopher Gandrud (간드루드 크리스토파)**, and kindly contributed to R-bloggers)

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`

Year <- 1980:1999

# Dummy covariates

A <- B <- 1:20

Data1 <- data.frame(Year, A, B)

head(Data1)

`## Year A B`

## 1 1980 1 1

## 2 1981 2 2

## 3 1982 3 3

## 4 1983 4 4

## 5 1984 5 5

## 6 1985 6 6

Now let's lag the `A`

variable by one time unit.

`library(DataCombine)`

DataSlid1 <- slide(Data1, Var = "A", slideBy = -1)

head(DataSlid1)

`## Year A B A-1`

## 1 1980 1 1 NA

## 2 1981 2 2 1

## 3 1982 3 3 2

## 4 1983 4 4 3

## 5 1984 5 5 4

## 6 1985 6 6 5

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`

Year <- rep(1980:1983, 5)

ID <- sort(rep(seq(1:5), 4))

# Dummy covariates

A <- B <- 1:20

Data2 <- data.frame(Year, ID, A, B)

head(Data2)

`## Year ID A B`

## 1 1980 1 1 1

## 2 1981 1 2 2

## 3 1982 1 3 3

## 4 1983 1 4 4

## 5 1980 2 5 5

## 6 1981 2 6 6

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",`

slideBy = 2)

head(DataSlid2)

`## Year ID A B B2`

## 1 1980 1 1 1 3

## 2 1981 1 2 2 4

## 3 1982 1 3 3 NA

## 4 1983 1 4 4 NA

## 5 1980 2 5 5 7

## 6 1981 2 6 6 8

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 on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...