lead & lag R Functions of dplyr Package (2 Examples)

 

This tutorial shows how to apply the lead and lag functions of the dplyr add-on package in the R programming language.

Table of contents:

Let’s get started.

 

Example Data

As first step, we have to install and load the dplyr package:

install.packages("dplyr")                        # Install dplyr
library("dplyr")                                 # Load dplyr

Furthermore, we have to create some data for the examples of this R tutorial:

x <- 1:10                                        # Example vector

Our data is a simple numeric vector with a range from 1 to 10. Now, let’s apply the lead and lag functions to this vector!

 

Example 1: Basic Application of lead & lag Functions

If we want to conduct a basic application of lead and lag, we simply have to insert our example vector into the functions. We can apply lead as follows…

lead(x)                                          # Basic application of lead
# 2  3  4  5  6  7  8  9 10 NA

…and lag as follows:

lag(x)                                           # Basic application of lag
# NA  1  2  3  4  5  6  7  8  9

As you can see based on the previous RStudio console outputs, the lead function shifted our vector one element to the right side (i.e. cut off the first value and added an NA at the end) and the lag function shifted our vector one element to the left (i.e. cut off the last value and appended an NA at the beginning).

 

Example 2: Apply lead & lag Functions with Larger n

In practice, we often want to create leads and lags of more than one element of our vector. We can simply do that by specifying the number of steps within the lead…

lead(x, 3)                                       # Apply lead function with larger n
# 4  5  6  7  8  9 10 NA NA NA

…and lag functions:

lag(x, 3)                                        # Apply lag function with larger n
# NA NA NA  1  2  3  4  5  6  7

Such operations are especially useful for time series data, where we want to predict the future.

 

Video, Further Resources & Summary

Have a look at the following video of my YouTube channel. In the video, I’m explaining the R programming syntax of this tutorial:

 

The YouTube video will be added soon.

 

In addition, you could have a look at some of the other articles of this website. You can find some other articles about the dplyr package and related functions below.

 

At this point you should know how to create leads and lags in the R programming language. In case you have additional questions, don’t hesitate to tell me about it in the comments section.

 



Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Menu
Top