The tsibble package provides a data class of tbl_ts to store and manage temporal-context data frames in a tidy manner. A tsibble consists of a time index, keys and other measured variables in a data-centric format, which is built on top of the tibble.

Installation

You could install the stable version on CRAN:

You could install the development version from Github using

# install.packages("devtools")
devtools::install_github("earowang/tsibble", build_vignettes = TRUE)

Get started

Coerce to a tsibble with as_tsibble()

The weather data included in the package nycflights13 is used as an example to illustrate. The “index” variable is the time_hour containing the date-times, and the “key” is the origin as weather stations created via the id(). The key(s) together with the index uniquely identifies each observation, which gives a valid tsibble. Other columns can be considered as measured variables.

The key is not constrained to a single variable, but expressive of nested and crossed data structures. This incorporates univariate, multivariate, hierarchical and grouped time series into the tsibble framework. See ?tsibble and vignette("intro-tsibble") for details.

tsummarise() to summarise over calendar periods

A new verb tsummarise() is introduced to aggregate interested variables over calendar periods. The tsummarise goes hand in hand with the index functions including as.Date(), yearmonth(), and yearquarter(), as well as other friends from lubridate, such as year() and ceiling_date(). For example, it would be of interest in computing average temperature and total precipitation per month, by applying the yearmonth() to the hourly time index.

The tsummarise() can also help with regularising a tsibble of irregular time space.

A family of window functions: slide(), tile(), stretch()

Temporal data often involves moving window calculations. Several functions in the tsibble allow for different variations of moving windows using purrr-like syntax:

  • slide(): sliding window with overlapping observations.
  • tile(): tiling window without overlapping observations.
  • stretch(): fixing an initial window and expanding more observations.

For example, a moving average of window size 3 is carried out on hourly temperatures for each group (origin).

It can be noticed that the common dplyr verbs, such as summarise(), mutate(), select(), filter(), and arrange(), seamlessly work with the tsibble.

fill_na() to turn implicit missing values into explicit missing values

Often there are implicit missing cases in temporal data. If the observations are made at regular time interval, we could turn these implicit missings to be explicit. The fill_na() function not only completes the index and keys to make the NAs present, but also provides a consistent interface to replace these NAs using a set of name-value pairs.

In the example of pedestrian, the missing values of the Date and Time, are supplied by the corresponding component of the Date_Time. The rest of untouched variables (i.e. Count) simply leave NA as is.