How to create a lagged variable in R for groups?


Lagged variable is the type of variable that contains the previous value of the variable for which we want to create the lagged variable and the first value is neglected. Therefore, we will always have one missing value in each of the groups, if we are creating a lagged variable that depends on a grouping variable or factor variable.

Example

Consider the below data frame:

> set.seed(2)
> Factor<-rep(c("F1","F2","F3","F4"),each=5)
> Rate<-c(12,54,18,26,14,25,81,47,15,12,96,25,14,34,68,78,42,56,81,75)
> df<-data.frame(Factor,Rate)
> df
Factor Rate
1 F1 12
2 F1 54
3 F1 18
4 F1 26
5 F1 14
6 F2 25
7 F2 81
8 F2 47
9 F2 15
10 F2 12
11 F3 96
12 F3 25
13 F3 14
14 F3 34
15 F3 68
16 F4 78
17 F4 42
18 F4 56
19 F4 81
20 F4 75

Creating a lagged variable for Rate treating Factor variable as group variable −

> df$Lagged_Variable <- c(NA, df$Rate[-nrow(df)])
> df$Lagged_Variable[which(!duplicated(df$Factor))] <- NA
> df
Factor Rate Lagged_Variable
1 F1 12 NA
2 F1 54 12
3 F1 18 54
4 F1 26 18
5 F1 14 26
6 F2 25 NA
7 F2 81 25
8 F2 47 81
9 F2 15 47
10 F2 12 15
11 F3 96 NA
12 F3 25 96
13 F3 14 25
14 F3 34 14
15 F3 68 34
16 F4 78 NA
17 F4 42 78
18 F4 56 42
19 F4 81 56
20 F4 75 81

Updated on: 12-Aug-2020

345 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements