Learn R Programming

dataprep (version 0.1.8)

log_returns: 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)

Value

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

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.

Examples

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

Run the code above in your browser using DataLab