Skip to contents

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.

Value

Returns a tibble object.

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.

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)
#> Warning: Non-numeric columns being dropped: date

# No index
as.data.frame(data_ts)
#>     x
#> 1 100
#> 2 105
#> 3 110
#> 4 115
#> 5 120

# Defualt index returned is regularized numeric index
tk_tbl(data_ts)
#> # A tibble: 5 × 2
#>   index     x
#>   <dbl> <dbl>
#> 1 2010    100
#> 2 2010.   105
#> 3 2010.   110
#> 4 2010.   115
#> 5 2010.   120

# Original date index returned (Only possible if original data has time-based index)
tk_tbl(data_ts, timetk_idx = TRUE)
#> # A tibble: 5 × 2
#>   index          x
#>   <date>     <dbl>
#> 1 2010-01-01   100
#> 2 2010-01-02   105
#> 3 2010-01-03   110
#> 4 2010-01-04   115
#> 5 2010-01-05   120


### xts to tibble: Comparison between as.data.frame() and tk_tbl()
data_xts <- tk_xts(data_tbl)
#> Warning: Non-numeric columns being dropped: date
#> Using column `date` for date_var.

# Dates are character class stored in row names
as.data.frame(data_xts)
#>              x
#> 2010-01-01 100
#> 2010-01-02 105
#> 2010-01-03 110
#> 2010-01-04 115
#> 2010-01-05 120

# Dates are appropriate date class and within the data frame
tk_tbl(data_xts)
#> # A tibble: 5 × 2
#>   index          x
#>   <date>     <dbl>
#> 1 2010-01-01   100
#> 2 2010-01-02   105
#> 3 2010-01-03   110
#> 4 2010-01-04   115
#> 5 2010-01-05   120


### 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)
#>         data_zooreg
#> 2000 Q1           1
#> 2000 Q2           2
#> 2000 Q3           3
#> 2000 Q4           4
#> 2001 Q1           5
#> 2001 Q2           6
#> 2001 Q3           7
#> 2001 Q4           8

# Dates are appropriate zoo yearqtr class within the data frame
tk_tbl(data_zooreg)
#> # A tibble: 8 × 2
#>   index     value
#>   <yearqtr> <int>
#> 1 2000 Q1       1
#> 2 2000 Q2       2
#> 3 2000 Q3       3
#> 4 2000 Q4       4
#> 5 2001 Q1       5
#> 6 2001 Q2       6
#> 7 2001 Q3       7
#> 8 2001 Q4       8


### 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)
#>          data_zoo
#> Jan 2016        1
#> Feb 2016        2
#> Mar 2016        3
#> Apr 2016        4
#> May 2016        5
#> Jun 2016        6
#> Jul 2016        7
#> Aug 2016        8
#> Sep 2016        9
#> Oct 2016       10
#> Nov 2016       11
#> Dec 2016       12

# Dates are appropriate zoo yearmon class within the data frame
tk_tbl(data_zoo)
#> # A tibble: 12 × 2
#>    index     value
#>    <yearmon> <int>
#>  1 Jan 2016      1
#>  2 Feb 2016      2
#>  3 Mar 2016      3
#>  4 Apr 2016      4
#>  5 May 2016      5
#>  6 Jun 2016      6
#>  7 Jul 2016      7
#>  8 Aug 2016      8
#>  9 Sep 2016      9
#> 10 Oct 2016     10
#> 11 Nov 2016     11
#> 12 Dec 2016     12