Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
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 −
xThe 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.47741638To create lagged column of x, add the following code to the above snippet −
xOutput
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 NAExample 2
Following snippet creates a sample data frame −
yThe 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 1To create lagged column of y, add the following code to the above snippet −
yOutput
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
