tk_tbl: Coerce time-series objects to tibble.

View source: R/coersion-tk_tbl.R

tk_tblR Documentation

Coerce time-series objects to tibble.

Description

Coerce time-series objects to tibble.

Usage

tk_tbl(
  data,
  preserve_index = TRUE,
  rename_index = "index",
  timetk_idx = FALSE,
  silent = FALSE,
  ...
)

Arguments

data

A time-series object.

preserve_index

Attempts to preserve a time series index. Default is TRUE.

rename_index

Enables the index column to be renamed.

timetk_idx

Used to return a date / datetime index for regularized objects that contain a timetk "index" attribute. Refer to tk_index() for more information on returning index information from regularized timeseries objects (i.e. ts).

silent

Used to toggle printing of messages and warnings.

...

Additional parameters passed to the tibble::as_tibble() function.

Details

tk_tbl is designed to coerce time series objects (e.g. xts, zoo, ts, timeSeries, etc) to tibble objects. The main advantage is that the function keeps the date / date-time information from the underlying time-series object.

When preserve_index = TRUE is specified, a new column, index, is created during object coercion, and the function attempts to preserve the date or date-time information. The date / date-time column name can be changed using the rename_index argument.

The timetk_idx argument is applicable when coercing ts objects that were created using tk_ts() from an object that had a time base (e.g. tbl, xts, zoo). Setting timetk_idx = TRUE enables returning the timetk "index" attribute if present, which is the original (non-regularized) time-based index.

Value

Returns a tibble object.

See Also

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

Examples

library(tidyverse)
library(timetk)

data_tbl <- tibble(
    date = seq.Date(from = as.Date("2010-01-01"), by = 1, length.out = 5),
    x    = seq(100, 120, by = 5)
)


### ts to tibble: Comparison between as.data.frame() and tk_tbl()
data_ts <- tk_ts(data_tbl, start = c(2010,1), freq = 365)

# No index
as.data.frame(data_ts)

# Defualt index returned is regularized numeric index
tk_tbl(data_ts)

# Original date index returned (Only possible if original data has time-based index)
tk_tbl(data_ts, timetk_idx = TRUE)


### xts to tibble: Comparison between as.data.frame() and tk_tbl()
data_xts <- tk_xts(data_tbl)

# Dates are character class stored in row names
as.data.frame(data_xts)

# Dates are appropriate date class and within the data frame
tk_tbl(data_xts)


### zooreg to tibble: Comparison between as.data.frame() and tk_tbl()
data_zooreg <- tk_zooreg(1:8, start = zoo::yearqtr(2000), frequency = 4)

# Dates are character class stored in row names
as.data.frame(data_zooreg)

# Dates are appropriate zoo yearqtr class within the data frame
tk_tbl(data_zooreg)


### zoo to tibble: Comparison between as.data.frame() and tk_tbl()
data_zoo <- zoo::zoo(1:12, zoo::yearmon(2016 + seq(0, 11)/12))

# Dates are character class stored in row names
as.data.frame(data_zoo)

# Dates are appropriate zoo yearmon class within the data frame
tk_tbl(data_zoo)





timetk documentation built on June 1, 2022, 1:07 a.m.