| log_returns | R Documentation |
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.
log_returns(data, cols = NULL, group = NULL, verbose = FALSE)
data |
A data frame, matrix, or numeric vector. |
cols |
Column indices or names of numeric variables. If |
group |
Optional grouping column for group-specific returns. |
verbose |
Logical; if |
The function only computes a return when both the current and previous values are positive. Otherwise, the result is NA.
A vector or data frame with logarithmic returns. The first observation of each group (or the vector) is NA.
log_returns(data[1:200, c(1, 4, 17:19)], cols = 3:5)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.