How to create a lagged column in an R data frame?

R ProgrammingServer Side ProgrammingProgramming

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
raja
Updated on 12-Nov-2021 07:48:02

Advertisements