View source: R/coersion-tk_ts.R
tk_ts | R Documentation |
Coerce time series objects and tibbles with date/date-time columns to ts.
tk_ts(
data,
select = NULL,
start = 1,
end = numeric(),
frequency = 1,
deltat = 1,
ts.eps = getOption("ts.eps"),
silent = FALSE
)
tk_ts_(
data,
select = NULL,
start = 1,
end = numeric(),
frequency = 1,
deltat = 1,
ts.eps = getOption("ts.eps"),
silent = FALSE
)
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 |
start |
the time of the first observation. Either a single number or a vector of two numbers (the second of which is an integer), which specify a natural time unit and a (1-based) number of samples into the time unit. See the examples for the use of the second form. |
end |
the time of the last observation, specified in the same way
as |
frequency |
the number of observations per unit of time. |
deltat |
the fraction of the sampling period between successive
observations; e.g., 1/12 for monthly data. Only one of
|
ts.eps |
time series comparison tolerance. Frequencies are
considered equal if their absolute difference is less than
|
silent |
Used to toggle printing of messages and warnings. |
tk_ts()
is a wrapper for stats::ts()
that is designed
to coerce tibble
objects that have a "time-base" (meaning the values vary with time)
to ts
class objects. There are two main advantages:
Non-numeric columns get removed instead of being populated by NA's.
The returned ts
object retains a "timetk index" (and various other attributes) if detected.
The "timetk index" can be used to coerce between tbl
, xts
, zoo
, and ts
data types.
The select
argument is used to select subsets
of columns from the incoming data.frame.
Only columns containing numeric data are coerced. At a minimum, a frequency
and a start
should be specified.
For non-data.frame object classes (e.g. xts
, zoo
, timeSeries
, etc) the objects are coerced
using stats::ts()
.
tk_ts_
is a nonstandard evaluation method.
Returns a ts
object.
tk_index()
, tk_tbl()
, tk_xts()
, tk_zoo()
, tk_zooreg()
library(dplyr)
### tibble to ts: Comparison between tk_ts() and stats::ts()
data_tbl <- tibble::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))
# as.ts: Character columns introduce NA's; Result does not retain index
stats::ts(data_tbl[,-1], start = 2016)
# tk_ts: Only numeric columns get coerced; Result retains index in numeric format
data_ts <- tk_ts(data_tbl, start = 2016)
data_ts
# timetk index
tk_index(data_ts, timetk_idx = FALSE) # Regularized index returned
tk_index(data_ts, timetk_idx = TRUE) # Original date index returned
# Coerce back to tibble
data_ts %>% tk_tbl(timetk_idx = TRUE)
### Using select
tk_ts(data_tbl, select = y)
### NSE: Enables programming
select <- "y"
tk_ts_(data_tbl, select = select)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.