Skip to contents

Computes a trailing (right-aligned) moving average of value and appends those rows to the original data (long form).

Usage

econ_calc_trail_avg(df, trail_amount)

Arguments

df

An econanalyzr-valid tibble/data frame.

trail_amount

Positive integer-like window size.

Value

A tibble containing the original rows and the trailing-average rows.

Details

  • Input must pass check_econanalyzr_df().

  • trail_amount must be a positive, integer-ish scalar (e.g., 3L, 6).

  • If df is a grouped tibble (via dplyr::group_by()), the trailing average is computed within each group. If ungrouped, it is computed over the entire table.

  • Derived rows have data_transform_text appended with "; Trail {n}".

  • Windows are right-aligned with .complete = TRUE: the first trail_amount - 1 rows per group are NA.