Skip to content
exuber

Helpers

Retrieve/Replace the index

Also: index, index<-

index-rd
index(x, ...)

index(x) <- value

Retrieve or replace the index of an object.

Arguments

x An object.
... Further arguments passed to methods.
value An ordered vector of the same length as the index attribute of x.

Value

The index of the object (a Date vector when the input data carried one, otherwise an integer sequence). The replacement form returns the modified object.

Details

If you do not specify an index for the estimation, the function generates a pseudo-index, a sequential numeric series. After the estimation you can use index() to retrieve the index or index<-() to replace it. The index can be numeric or Date.

Examples

These examples are copied from the package's own documentation and are run by R CMD check on every release. The printed output (after #>) and the plots were produced by running them against the current package source.

# A plain numeric vector gets a pseudo-index (1, 2, 3, ...)
rsim <- radf(sim_data)
head(index(rsim))
#> [1] 1 2 3 4 5 6

# A data.frame with a Date column uses it as the index automatically
rsim_wdate <- radf(sim_data_wdate)
#> Using `date` as index variable.
head(index(rsim_wdate))
#> [1] "2000-01-01" "2000-02-01" "2000-03-01" "2000-04-01" "2000-05-01"
#> [6] "2000-06-01"
class(index(rsim_wdate))
#> [1] "Date"

# autoplot() uses index() internally for the x-axis
autoplot(rsim_wdate)
#> Using precomputed critical values for `cv`.
Plot from the index-rd example
# Replace the index, e.g. with a custom Date sequence
index(rsim) <- seq(as.Date("2000-01-01"), by = "month", length.out = length(index(rsim)))
head(index(rsim))
#> [1] "2000-01-01" "2000-02-01" "2000-03-01" "2000-04-01" "2000-05-01"
#> [6] "2000-06-01"
autoplot(rsim)
#> Using precomputed critical values for `cv`.
Plot from the index-rd example