index_series: Index one or more series

View source: R/index_series.R

index_seriesR Documentation

Index one or more series

Description

Rescale numeric series relative to their earliest observed value or to the arithmetic mean over a selected base period.

Usage

index_series(
  data,
  date_col = "date",
  value_col = "value",
  group_cols = NULL,
  base_period = NULL,
  base_value = 100,
  na_rm = FALSE,
  suffix = NULL,
  .quiet = FALSE
)

Arguments

data

A non-empty data.frame, tibble, or data.table.

date_col

Name of the Date column. Defaults to "date".

value_col

Non-empty character vector naming numeric value columns.

group_cols

Optional character vector naming grouping columns. Each group receives its own reference value.

base_period

NULL to use the earliest non-missing observation; one or two four-digit integer years; one or two Date values; or the name of a Date column holding one base date per group. Two values define an inclusive range and may be supplied in either order. A single date selects the calendar period containing it at the detected frequency.

base_value

Finite positive number assigned to the reference. Defaults to 100.

na_rm

Whether to remove missing values when averaging an explicit base period. With the default FALSE, a missing base observation raises an error instead of blanking the entire indexed series. This argument has no effect when base_period = NULL.

suffix

Optional non-missing character suffix for generated names.

.quiet

If TRUE, suppress frequency-detection messages. Warnings about incomplete base periods are never suppressed.

Details

When base_period is supplied, dates are matched at the detected calendar frequency of each group. Thus, for monthly data, as.Date("2019-01-01") also matches an observation dated at month end. Weekly and daily series use exact interval containment. A partly observed base interval produces a warning. With base_period = NULL, a warning identifies any series whose first dated value is missing and whose reference moves to a later date.

Value

A tibble containing the original columns, in their original order, followed by ⁠index_{value_col}⁠ columns (and ⁠_{suffix}⁠ when supplied).

See Also

augment_trends() for trend estimation and augment_rolling() for rolling and year-to-date aggregations.

Examples

vehicles |>
  index_series(value_col = "production")

retail_volume |>
  index_series(group_cols = "name_series", base_period = 2019)


trendseries documentation built on Oct. 1, 2026, 5:10 p.m.