lead function in R: Set the NA values at the end of the R object

lead in r

In this guide, we will share with you what the lead() function does in R and how to use this function. The lead in R helps you set the NA values at the end of the R object. Follow us to learn how to perform the lead() function with the explanation below.

What does lead() do in R?

The lead() function is built in the ‘dplyr’ package and it is used to set the NA values with the specified number of elements from the ending of the R object like the vector, the data frame, … You can also set the NA values with the specified number of elements from the beginning of an object by the lag() function in my previous tutorial. Now, take a look at the syntax of the lead() function.

Syntax:

dplyr :: lead(data, length)

Parameters:

  • data: The R object.
  • length: The number of elements you want to set NA values from the ending of the object.

After learning what the lead in R does and the syntax of this function, we will show you how to perform it in the next title below.

How to use the lead in R?

To use the lead in R, you must install the ‘dplyr’ package first by the following command.

install.packages('dplyr')

Lead function with the vector

Using the lead() function to set the NA values at the end of the vector. 

Look at the example below.

# Create the vector
vec <- c(5, 3, 4, 6, 8, 3, 2)

# Set a NA value at the end of the vector
dplyr::lead(vec)

Output

[1]  3  4  6  8  3  2 NA

Set multiple NA values with the lead function in R

You can set multiple NA values at the end of the vector with the lead function by assigning a value to the ‘length’ parameter.

Look at the example below.

# Create the vector
vec <- c(5, 3, 4, 6, 8, 3, 2)

# Set 3 NA values at the end of the vector
dplyr::lead(vec, 3)

Output

[1]  6  8  3  2 NA NA NA

Lead function with the data frame

You can also use the lead function with the data frame to set the NA values at the end of all columns.

Look at the example below.

# Create the data frame.
df <- data.frame(
    season = c(2010, 2011, 2012, 2013, 2014),
    champion = c("RMA", "BAR", "RMA", "BAY", "RMA")
)

dplyr::lead(df)

Output

   season champion
1   2011      BAR
2   2012      RMA
3   2013      BAY
4   2014      RMA
5     NA     <NA>

Set multiple NA values in the data frame

Like the vector, you can also set multiple NA values in the data frame.

Look at the example below.

# Create the data frame
df <- data.frame(
    season = c(2010, 2011, 2012, 2013, 2014),
    champion = c("RMA", "BAR", "RMA", "BAY", "RMA")
)

# Set 2 NA values at the end of all columns
dplyr::lead(df, 2)

Output

   season champion
1   2012      RMA
2   2013      BAY
3   2014      RMA
4     NA     <NA>
5     NA     <NA>

Summary

We have shown you the definition, the syntax, and how to perform the lead in R. The lead() function in R is used to set the NA values at the end of the R object. We hope this tutorial is helpful to you. Thanks!

Maybe you are interested:

Posted in R

Leave a Reply

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