R Add Lagged Column by Group to Data Frame Using dplyr (Example Code)
In this R tutorial you’ll learn how to add a column with lagged values by group to a data frame.
Example Data
data(iris) # Load & modify example data my_iris <- iris[c(1:3, 51:53, 101:103), c("Sepal.Length", "Species")] my_iris # Display example data in RStudio # Sepal.Length Species # 1 5.1 setosa # 2 4.9 setosa # 3 4.7 setosa # 51 7.0 versicolor # 52 6.4 versicolor # 53 6.9 versicolor # 101 6.3 virginica # 102 5.8 virginica # 103 7.1 virginica |
data(iris) # Load & modify example data my_iris <- iris[c(1:3, 51:53, 101:103), c("Sepal.Length", "Species")] my_iris # Display example data in RStudio # Sepal.Length Species # 1 5.1 setosa # 2 4.9 setosa # 3 4.7 setosa # 51 7.0 versicolor # 52 6.4 versicolor # 53 6.9 versicolor # 101 6.3 virginica # 102 5.8 virginica # 103 7.1 virginica
Example: Adding Lagged Column for Each Group Using group_by, mutate & lag Functions
install.packages("dplyr") # Install & load dplyr library("dplyr") |
install.packages("dplyr") # Install & load dplyr library("dplyr")
my_iris %>% # Add lagged column group_by(Species) %>% dplyr::mutate(my_lag = dplyr::lag(Sepal.Length, n = 1, default = NA)) # # A tibble: 9 x 3 # # Groups: Species [3] # Sepal.Length Species my_lag # <dbl> <fct> <dbl> # 1 5.1 setosa NA # 2 4.9 setosa 5.1 # 3 4.7 setosa 4.9 # 4 7 versicolor NA # 5 6.4 versicolor 7 # 6 6.9 versicolor 6.4 # 7 6.3 virginica NA # 8 5.8 virginica 6.3 # 9 7.1 virginica 5.8 |
my_iris %>% # Add lagged column group_by(Species) %>% dplyr::mutate(my_lag = dplyr::lag(Sepal.Length, n = 1, default = NA)) # # A tibble: 9 x 3 # # Groups: Species [3] # Sepal.Length Species my_lag # <dbl> <fct> <dbl> # 1 5.1 setosa NA # 2 4.9 setosa 5.1 # 3 4.7 setosa 4.9 # 4 7 versicolor NA # 5 6.4 versicolor 7 # 6 6.9 versicolor 6.4 # 7 6.3 virginica NA # 8 5.8 virginica 6.3 # 9 7.1 virginica 5.8