# lag function in R: How to use lag function in R

In this article, we will show you how to use the lag function in R. The lag() function in R can help you set the NA value by the specified beginning indexes in the vector, the array, or the data frame. Let’s follow this article to learn more about it with the explanation and examples below.

## Lag function in R

What does lag() do in R?

The lag function in R built-in ‘dplyr’ package can help you set the NA value by the specified beginning indexes in the vector, the array, or the data frame. The same functionally, you can use the lead function that is also built-in ‘dplyr’ package to set the NA value by the specified ending indexes in an object.

Syntax:

dplyr :: lag(object, size)

Parameters:

• object: The vector, the array, or the data frame.
• size: The specified length you want to set the NA value in from the beginning object.

After learning the usage and syntax of the lag() function in R, you will learn how to use it in the next title below.

## How to use the lag function in R?

To use the lag() function in R, you have to install the ‘dplyr’ package first because it is built into this package. You can install this package by the following statement.

install.packages('dplyr')

### Use the lag function to set one NA value in the vector

You can use the lag function to set the NA value in the vector. By default, you will set the first element to NA value.

Look at the example below.

# Create a vector
vector <- c(1,2,3,4,5,6,7,8,9,10)

dplyr::lag(vector)

Output:

[1] NA 1 2 3 4 5 6 7 8 9

### Use the lag function to set multiple NA values in the vector

By default, you only set one NA value for the first element. If you want to set multiple values to NA from the beginning, you have to set the second parameter, that is the amount of the NA value from the beginning.

Look at the example below:

vector <- c(1,2,3,4,5,6,7,8,9,10)

# Set four elements from the beginning to NA
dplyr::lag(vector,4)

Output:

[1] NA NA NA NA 1 2 3 4 5 6

### Use the lag function to set one NA value in each column in the data frame

You can use the lag function to set the NA value in each column in the data frame. But you can flexibly use it to set the NA value in the specified column the same as the vector.

Look at the example below:

df <- data.frame( Name = c('Haley', 'Joker', 'Batman', 'Spider-Man', 'Hulk'),
Age = c(20, 30, 35, 40, 24) )

dplyr :: lag(df)

Output:

        Name Age
1       <NA>  NA
2      Haley  20
3      Joker  30
4     Batman  35
5 Spider-Man  40

### Use the lag function to set multiple NA values in each column in the data frame

You can use the lag function to set multiple NA values in each column in the data frame.

Look at the example below:

df <- data.frame( Name = c('Haley', 'Joker', 'Batman', 'Spider-Man', 'Hulk'),
Age = c(20, 30, 35, 40, 24) )

dplyr :: lag(df,2)

Output:

    Name Age
1   <NA>  NA
2   <NA>  NA
3  Haley  20
4  Joker  30
5 Batman  35

## Summary

You have learned about the lag function built-in ‘dplyr’ package in R. By this function, you can set the NA values in an object by the specified length from the beginning of the object. If you have any questions about this tutorial, leave a comment below, and I will answer your questions. Thanks!

Maybe you are interested:

Posted in R