- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to create a lagged column in an R data frame?
To create a lagged column in an R data frame, we can use transform function.
For example, if we have a data frame called that contains a column say C and we want to create a lagged column in df based on C then we can use the command given below −
transform(df,Lag_C=c(C[-1],NA))
Example 1
Following snippet creates a sample data frame −
x<-rnorm(20) df1<-data.frame(x) df1
The following dataframe is created −
x 1 -0.58699645 2 -1.74183976 3 -0.80306205 4 -0.80224690 5 0.01596175 6 -0.36427931 7 -0.33449380 8 1.62647476 9 -1.71252529 10 0.02802091 11 1.27800967 12 0.41098805 13 -1.46857510 14 -0.76261265 15 0.16337484 16 -0.75678517 17 -0.26163702 18 0.75312452 19 0.40915108 20 -2.47741638
To create lagged column of x, add the following code to the above snippet −
x<-rnorm(20) df1<-data.frame(x) transform(df1,Lagged_x=c(x[-1],NA))
Output
If you execute all the above given snippets as a single program, it generates the following output −
x Lagged_x 1 -0.58699645 -1.74183976 2 -1.74183976 -0.80306205 3 -0.80306205 -0.80224690 4 -0.80224690 0.01596175 5 0.01596175 -0.36427931 6 -0.36427931 -0.33449380 7 -0.33449380 1.62647476 8 1.62647476 -1.71252529 9 -1.71252529 0.02802091 10 0.02802091 1.27800967 11 1.27800967 0.41098805 12 0.41098805 -1.46857510 13 -1.46857510 -0.76261265 14 -0.76261265 0.16337484 15 0.16337484 -0.75678517 16 -0.75678517 -0.26163702 17 -0.26163702 0.75312452 18 0.75312452 0.40915108 19 0.40915108 -2.47741638 20 -2.47741638 NA
Example 2
Following snippet creates a sample data frame −
y<-rpois(20,6) df2<-data.frame(y) df2
The following dataframe is created −
y 1 8 2 4 3 4 4 6 5 5 6 10 7 6 8 2 9 8 10 3 11 6 12 6 13 12 14 9 15 1 16 5 17 13 18 5 19 5 20 1
To create lagged column of y, add the following code to the above snippet −
y<-rpois(20,6) df2<-data.frame(y) transform(df2,Lagged_y=c(y[-1],NA))
Output
If you execute all the above given snippets as a single program, it generates the following output −
y Lagged_y 1 8 4 2 4 4 3 4 6 4 6 5 5 5 10 6 10 6 7 6 2 8 2 8 9 8 3 10 3 6 11 6 6 12 6 12 13 12 9 14 9 1 15 1 5 16 5 13 17 13 5 18 5 5 19 5 1 20 1 NA
- Related Articles
- How to create a group column in an R data frame?
- How to create a column in an R data frame with cumulative sum?
- How to create a blank column with randomization in an R data frame?
- How to concatenate column values and create a new column in an R data frame?
- How to create histogram for discrete column in an R data frame?
- How to create a duplicate column in an R data frame with different name?
- How to create a frequency column for categorical variable in an R data frame?
- Create a quartile column for each value in an R data frame column.
- How to standardized a column in an R data frame?
- How to create a row sum and a row product column in an R data frame?
- How to remove a column from an R data frame?
- How to rename a single column in an R data frame?
- How to replace a complete column in an R data frame?
- How to remove a character in an R data frame column?
- How to extract a single column of an R data frame as a data frame?
