tk_index: Extract an index of date or datetime from time series...

Description Usage Arguments Details Value See Also Examples

View source: R/tk_index.R

Description

Extract an index of date or datetime from time series objects, models, forecasts

Usage

1
2
3
tk_index(data, timekit_idx = FALSE, silent = FALSE)

has_timekit_idx(data)

Arguments

data

A time-based tibble, time-series object, time-series model, or forecast object.

timekit_idx

If timekit_idx is TRUE a timekit time-based index attribute is attempted to be returned. If FALSE the default index is returned. See discussion below for further details.

silent

Used to toggle printing of messages and warnings.

Details

tk_index() is used to extract the date or datetime index from various time series objects, models and forecasts. The method can be used on tbl, xts, zoo, zooreg, and ts objects. The method can additionally be used on forecast objects and a number of objects generated by modeling functions such as Arima, ets, and HoltWinters classes to get the index of the underlying data.

The boolean timekit_idx argument is applicable to regularized time series objects such as ts and zooreg classes that have both a regularized index and potentially a "timekit index" (a time-based attribute). When set to FALSE the regularized index is returned. When set to TRUE the time-based timekit index is returned if present.

has_timekit_idx() is used to determine if the object has a timekit index attribute and can thus benefit from the tk_index(timekit_idx = TRUE). TRUE indicates the "timekit index" attribute is present. FALSE indicates the "timekit index" attribute is not present. If FALSE, the tk_index() function will return the default index for the data type.

Important Note: To gain the benefit of timekit_idx the time series must have a timekit index. Use has_timekit_idx to determine if the object has a timekit index. This is particularly important for ts objects, which by default do not contain a time-based index and therefore must be coerced from time-based objects such as tbl, xts, or zoo using the tk_ts() function in order to get the "timekit index" attribute. Refer to tk_ts() for creating persistent date / datetime index during coercion to ts.

Value

Returns a vector of date or date times

See Also

tk_ts(), tk_tbl(), tk_xts(), tk_zoo(), tk_zooreg()

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
library(tidyverse)
library(timekit)

# Create time-based tibble
data_tbl <- tibble::tibble(
    date = seq.Date(from = as.Date("2000-01-01"), by = 1, length.out = 5),
    x    = rnorm(5) * 10,
    y    = 5:1
)
tk_index(data_tbl) # Returns time-based index vector

# Coerce to ts using tk_ts(): Preserves time-basis
data_ts <- tk_ts(data_tbl)
tk_index(data_ts, timekit_idx = FALSE) # Returns regularized index
tk_index(data_ts, timekit_idx = TRUE)  # Returns original time-based index vector

# Coercing back to tbl
tk_tbl(data_ts, timekit_idx = FALSE) # Returns regularized tbl
tk_tbl(data_ts, timekit_idx = TRUE)  # Returns time-based tbl

timekit documentation built on July 4, 2017, 9:45 a.m.