Description Usage Arguments Details Value See Also Examples
Coerce time series objects and tibbles with date/date-time columns to ts.
1 2 3 4 5 |
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 integers, 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 "timekit index" (and various other attributes) if detected.
The "timekit 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()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | library(tidyverse)
library(timekit)
### 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
# timekit index
tk_index(data_ts, timekit_idx = FALSE) # Regularized index returned
tk_index(data_ts, timekit_idx = TRUE) # Original date index returned
# Coerce back to tibble
data_ts %>% tk_tbl(timekit_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.