Stable lifecycle

tsibble(..., key = NULL, index, regular = TRUE, .drop = TRUE)

Arguments

...

A set of name-value pairs.

key

Unquoted variable(s) that uniquely determine time indices. NULL for empty key, and c() for multiple variables. It works with tidy selector (e.g. dplyr::starts_with()).

index

A bare (or unquoted) variable to specify the time index variable.

regular

Regular time interval (TRUE) or irregular (FALSE). The interval is determined by the greatest common divisor of index column, if TRUE.

.drop

If TRUE, empty key groups are dropped.

Value

A tsibble object.

Details

A tsibble is sorted by its key first and index.

Index

An extensive range of indices are supported by tsibble: native time classes in R (such as Date, POSIXct, and difftime) and tsibble's new additions (such as yearweek, yearmonth, and yearquarter). Some commonly-used classes have built-in support too, including ordered, hms::hms, zoo::yearmon, zoo::yearqtr, and nanotime.

For a tbl_ts of regular interval, a choice of index representation has to be made. For example, a monthly data should correspond to time index created by yearmonth or zoo::yearmon, instead of Date or POSIXct. Because months in a year ensures the regularity, 12 months every year. However, if using Date, a month containing days ranges from 28 to 31 days, which results in irregular time space. This is also applicable to year-week and year-quarter.

Tsibble supports arbitrary index classes, as long as they can be ordered from past to future. To support a custom class, you need to define index_valid() for the class and calculate the interval through interval_pull().

Key

Key variable(s) together with the index uniquely identifies each record:

  • Empty: an implicit variable. NULL resulting in a univariate time series.

  • A single variable: For example, data(pedestrian) use the bare Sensor as the key.

  • Multiple variables: For example, Declare key = c(Region, State, Purpose) for data(tourism). Key can be created in conjunction with tidy selectors like starts_with().

Interval

The interval function returns the interval associated with the tsibble.

  • Regular: the value and its time unit including "nanosecond", "microsecond", "millisecond", "second", "minute", "hour", "day", "week", "month", "quarter", "year". An unrecognisable time interval is labelled as "unit".

  • Irregular: as_tsibble(regular = FALSE) gives the irregular tsibble. It is marked with !.

  • Unknown: Not determined (?), if it's an empty tsibble, or one entry for each key variable.

An interval is obtained based on the corresponding index representation:

  • integerish numerics between 1582 and 2499: "year" (Y). Note the year of 1582 saw the beginning of the Gregorian Calendar switch.

  • yearquarter/yearqtr: "quarter" (Q)

  • yearmonth/yearmon: "month" (M)

  • yearweek: "week" (W)

  • Date: "day" (D)

  • difftime: "quarter" (Q), "month" (M), "week" (W), "day" (D), "hour" (h), "minute" (m), "second" (s)

  • POSIXt/hms: "hour" (h), "minute" (m), "second" (s), "millisecond" (us), "microsecond" (ms)

  • nanotime: "nanosecond" (ns)

  • other numerics &ordered (ordered factor): "unit" When the interval cannot be obtained due to the mismatched index format, an error is issued.

The interval is invariant to subsetting, such as filter(), slice(), and [.tbl_ts. But if the result is an empty tsibble, the interval is always unknown. When joining a tsibble with other data sources and aggregating to different time scales, the interval gets re-calculated.

See also

Examples

# create a tsibble w/o a key tsibble( date = as.Date("2017-01-01") + 0:9, value = rnorm(10) )
#> Using `date` as index variable.
#> # A tsibble: 10 x 2 [1D] #> date value #> <date> <dbl> #> 1 2017-01-01 0.0796 #> 2 2017-01-02 -1.26 #> 3 2017-01-03 1.03 #> 4 2017-01-04 -0.731 #> 5 2017-01-05 -0.190 #> 6 2017-01-06 0.529 #> 7 2017-01-07 0.550 #> 8 2017-01-08 0.550 #> 9 2017-01-09 -0.660 #> 10 2017-01-10 0.0574
# create a tsibble with a single variable for key tsibble( qtr = rep(yearquarter("2010 Q1") + 0:9, 3), group = rep(c("x", "y", "z"), each = 10), value = rnorm(30), key = group )
#> Using `qtr` as index variable.
#> # A tsibble: 30 x 3 [1Q] #> # Key: group [3] #> qtr group value #> <qtr> <chr> <dbl> #> 1 2010 Q1 x -2.81 #> 2 2010 Q2 x -0.912 #> 3 2010 Q3 x -0.782 #> 4 2010 Q4 x -0.664 #> 5 2011 Q1 x 0.626 #> 6 2011 Q2 x -0.507 #> 7 2011 Q3 x 0.270 #> 8 2011 Q4 x 0.467 #> 9 2012 Q1 x 0.724 #> 10 2012 Q2 x 0.614 #> # … with 20 more rows
# create a tsibble with multiple variables for key tsibble( mth = rep(yearmonth("2010 Jan") + 0:8, each = 3), xyz = rep(c("x", "y", "z"), each = 9), abc = rep(letters[1:3], times = 9), value = rnorm(27), key = c(xyz, abc) )
#> Using `mth` as index variable.
#> # A tsibble: 27 x 4 [1M] #> # Key: xyz, abc [9] #> mth xyz abc value #> <mth> <chr> <chr> <dbl> #> 1 2010 Jan x a -0.334 #> 2 2010 Feb x a 0.587 #> 3 2010 Mar x a 1.43 #> 4 2010 Jan x b 1.64 #> 5 2010 Feb x b -0.150 #> 6 2010 Mar x b -2.65 #> 7 2010 Jan x c -0.644 #> 8 2010 Feb x c -1.71 #> 9 2010 Mar x c -1.03 #> 10 2010 Apr y a -0.707 #> # … with 17 more rows
# create a tsibble containing "key" and "index" as column names tsibble(!!!list( index = rep(yearquarter("2010 Q1") + 0:9, 3), key = rep(c("x", "y", "z"), each = 10), value = rnorm(30)), key = key, index = index )
#> # A tsibble: 30 x 3 [1Q] #> # Key: key [3] #> index key value #> <qtr> <chr> <dbl> #> 1 2010 Q1 x -0.943 #> 2 2010 Q2 x -0.121 #> 3 2010 Q3 x 1.34 #> 4 2010 Q4 x -0.860 #> 5 2011 Q1 x 0.667 #> 6 2011 Q2 x -1.42 #> 7 2011 Q3 x 1.17 #> 8 2011 Q4 x -1.40 #> 9 2012 Q1 x 1.10 #> 10 2012 Q2 x 0.698 #> # … with 20 more rows