View source: R/join_lagged_values.R
| join_lagged_values | R Documentation |
Joins lagged values of selected variables from one dataset (new_data)
into another (original_data), based on date ranges defined by min_lag
and max_lag. Unlike add_lagged_columns(), this function supports
joining across data frames with different date grids (e.g., monthly source
data into quarterly target data).
join_lagged_values(
original_data,
new_data,
id_keys,
min_lag,
max_lag,
ff_adjustment = FALSE,
data_options = NULL
)
original_data |
A data frame containing the target panel data. |
new_data |
A data frame containing the source variables to lag and
merge. All columns besides |
id_keys |
A character vector specifying the identifier column(s). |
min_lag |
A |
max_lag |
A |
ff_adjustment |
Logical; if |
data_options |
A list of class |
A data frame with all columns from original_data plus the
lagged columns from new_data (keeping their original names).
Other rolling and lagging functions:
add_lagged_columns(),
compute_rolling_value()
set.seed(42)
library(dplyr)
library(lubridate)
df1 <- tibble(
id = rep(1:2, each = 6),
date = rep(seq(as.Date("2020-01-01"), by = "month", length.out = 6), 2)
)
df2 <- df1 |>
mutate(x = rnorm(n()))
join_lagged_values(
original_data = df1,
new_data = df2,
id_keys = "id",
min_lag = months(1),
max_lag = months(3)
)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.