Coerce time series objects and tibbles with date/date-time columns to xts.
Source:R/coersion-tk_zoo.R
tk_zoo.Rd
Coerce time series objects and tibbles with date/date-time columns to xts.
Usage
tk_zoo(data, select = NULL, date_var = NULL, silent = FALSE, ...)
tk_zoo_(data, select = NULL, date_var = NULL, silent = FALSE, ...)
Arguments
- data
A time-based tibble or time-series object.
- select
Applicable to tibbles and data frames only. The column or set of columns to be coerced to
ts
class.- date_var
Applicable to tibbles and data frames only. Column name to be used to
order.by
.NULL
by default. IfNULL
, function will find the date or date-time column.- silent
Used to toggle printing of messages and warnings.
- ...
Additional parameters to be passed to
xts::xts()
. Refer toxts::xts()
.
Details
tk_zoo
is a wrapper for zoo::zoo()
that is designed
to coerce tibble
objects that have a "time-base" (meaning the values vary with time)
to zoo
class objects. There are three main advantages:
Non-numeric columns that are not removed via
select
are dropped and the user is warned. This prevents an error or coercion issue from occurring.The date column is auto-detected if not specified by
date_var
. This takes the effort off the user to assign a date vector during coercion.ts
objects are automatically coerced if a "timetk index" is present. Refer totk_ts()
.
The select
argument can be used to select subsets
of columns from the incoming data.frame.
Only columns containing numeric data are coerced.
The date_var
can be used to specify the column with the date index.
If date_var = NULL
, the date / date-time column is interpreted.
Optionally, the order.by
argument from the underlying zoo::zoo()
function can be used.
The user must pass a vector of dates or date-times if order.by
is used.
Important Note: The ...
arguments are passed to xts::xts()
, which
enables additional information (e.g. time zone) to be an attribute of the zoo
object.
For non-data.frame object classes (e.g. xts
, zoo
, timeSeries
, etc) the objects are coerced
using zoo::zoo()
.
tk_zoo_
is a nonstandard evaluation method.
Examples
library(dplyr)
### tibble to zoo: Comparison between tk_zoo() and zoo::zoo()
data_tbl <- dplyr::tibble(
date = seq.Date(as.Date("2016-01-01"), by = 1, length.out = 5),
x = rep("chr values", 5),
y = cumsum(1:5),
z = cumsum(11:15) * rnorm(1))
# zoo: Characters will cause error; order.by must be passed a vector of dates
zoo::zoo(data_tbl[,-c(1,2)], order.by = data_tbl$date)
#> y z
#> 2016-01-01 1 -4.938873
#> 2016-01-02 3 -10.326734
#> 2016-01-03 6 -16.163584
#> 2016-01-04 10 -22.449422
#> 2016-01-05 15 -29.184248
# tk_zoo: Character columns dropped with a warning; No need to specify dates (auto detected)
tk_zoo(data_tbl)
#> Warning: Non-numeric columns being dropped: date, x
#> Using column `date` for date_var.
#> y z
#> 2016-01-01 1 -4.938873
#> 2016-01-02 3 -10.326734
#> 2016-01-03 6 -16.163584
#> 2016-01-04 10 -22.449422
#> 2016-01-05 15 -29.184248
# ts can be coerced back to zoo
data_tbl %>%
tk_ts(start = 2016, freq = 365) %>%
tk_zoo()
#> Warning: Non-numeric columns being dropped: date, x
#> Warning: 'tzone' attributes are inconsistent
#> y z
#> 2016-01-01 1 -4.938873
#> 2016-01-02 3 -10.326734
#> 2016-01-03 6 -16.163584
#> 2016-01-04 10 -22.449422
#> 2016-01-05 15 -29.184248
### Using select and date_var
tk_zoo(data_tbl, select = y, date_var = date)
#> y
#> 2016-01-01 1
#> 2016-01-02 3
#> 2016-01-03 6
#> 2016-01-04 10
#> 2016-01-05 15
### NSE: Enables programming
date_var <- "date"
select <- "y"
tk_zoo_(data_tbl, select = select, date_var = date_var)
#> y
#> 2016-01-01 1
#> 2016-01-02 3
#> 2016-01-03 6
#> 2016-01-04 10
#> 2016-01-05 15