Learn R Programming

dataprep (version 0.1.8)

detrend_ts: Remove linear trend from time series

Description

Fits a linear trend (intercept + slope) to each selected numeric column (or vector) and returns the residuals. This is useful for removing long-term trends before correlation analysis.

Usage

detrend_ts(data, cols = NULL, date_col = NULL, verbose = FALSE)

Value

A data frame or vector with the linear trend removed.

Arguments

data

A data frame, matrix, or numeric vector.

cols

Column indices or names to detrend. If NULL, all numeric columns are used.

date_col

Optional time column (not strictly required for linear detrending).

verbose

Logical; if TRUE, prints progress message.

Examples

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

Run the code above in your browser using DataLab