log_returns: Logarithmic returns for financial time series

View source: R/log_returns.R

log_returnsR Documentation

Logarithmic returns for financial time series

Description

Computes the logarithmic return \log(x_t / x_{t-1}) for selected numeric columns. Used extensively in financial time series analysis. Group-wise computation is supported.

Usage

log_returns(data, cols = NULL, group = NULL, verbose = FALSE)

Arguments

data

A data frame, matrix, or numeric vector.

cols

Column indices or names of numeric variables. If NULL, all numeric columns are used.

group

Optional grouping column for group-specific returns.

verbose

Logical; if TRUE, prints progress message.

Details

The function only computes a return when both the current and previous values are positive. Otherwise, the result is NA.

Value

A vector or data frame with logarithmic returns. The first observation of each group (or the vector) is NA.

Examples

log_returns(data[1:200, c(1, 4, 17:19)], cols = 3:5)

dataprep documentation built on Oct. 1, 2026, 5:07 p.m.