Learn R Programming

tidyfinance (version 0.4.3)

add_lag_columns: Add Lagged Versions of Columns to a Data Frame

Description

[Experimental]

This function adds lagged versions of specified columns to a data frame. Optionally, the operation can be grouped by another column and allows for flexible handling of missing values. The lag is applied based on the date column in the data frame.

Usage

add_lag_columns(
  data,
  cols,
  by = NULL,
  lag,
  max_lag = lag,
  drop_na = TRUE,
  data_options = NULL
)

Value

A data frame with lagged versions of the specified columns appended, optionally grouped by another column.

Arguments

data

A data frame containing the columns to be lagged.

cols

A character vector specifying the names of the columns to lag.

by

An optional column by which to group the data when applying the lag. Default is NULL, meaning no grouping.

lag

The number of periods to lag the columns by. Must be non-negative.

max_lag

An optional maximum lag period. The default is equal to lag.

drop_na

A logical value indicating whether to drop rows with missing values in the lagged columns. Default is TRUE.

data_options

A list of additional options for data processing, such as the date column. If NULL, defaults are used.

Examples

Run this code
# Create a sample data frame
data <- tibble::tibble(
  permno = rep(1:2, each = 10),
  date = rep(seq.Date(as.Date('2023-01-01'), by = "month", length.out = 10), 2),
  bm = runif(20, 0.5, 1.5),
  size = runif(20, 100, 200)
)

# Add lagged columns for 'bm' and 'size' with a 3-month lag, grouped by 'permno'
data |>
  add_lag_columns(c("bm", "size"), lag = months(3), by = "permno")

# Introduce missing values in the data
data$bm[c(3, 5, 7, 15, 18)] <- NA
data$size[c(2, 4, 8, 13)] <- NA

# Add lagged columns with NA values removed
data |>
  add_lag_columns(c("bm", "size"), lag = months(3), by = permno)

Run the code above in your browser using DataLab