The tsibble package provides a data infrastructure for tidy temporal data with wrangling tools. Adhering to the tidy data principles, tsibble is an explicit data- and model-oriented object. In tsibble:

  1. Index is a variable with inherent ordering from past to present.
  2. Key is a set of variables that define observational units over time.
  3. Each observation should be uniquely identified by index and key.
  4. Each observational unit should be measured at a common interval, if regularly spaced.

Installation

You could install the stable version on CRAN:

install.packages("tsibble")

You could install the development version from Github using

Get started

Coerce to a tsibble with as_tsibble()

To coerce a data frame to tsibble, we need to declare key and index. For example, in the weather data from the package nycflights13, the time_hour containing the date-times should be declared as index, and the origin as key. Other columns can be considered as measured variables.

The key can be comprised of empty, one, or more variables. See package?tsibble and vignette("intro-tsibble") for details.

Given time indices, the interval is obtained based on their time representation, ranging from year to nanosecond, from numerics to ordered factors. The POSIXct corresponds to sub-daily series, Date to daily, yearweek to weekly, yearmonth to monthly, yearquarter to quarterly, and etc.

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

Often there are implicit missing cases in time series. If the observations are made at regular time interval, we could turn these implicit missingness to be explicit simply using fill_gaps(), filling gaps in precipitation (precip) with 0 in the meanwhile. It is quite common to replaces NAs with its previous observation for each origin in time series analysis, which is easily done using fill() from tidyr.

fill_gaps() also handles filling in time gaps by values or functions, and respects time zones for date-times. Wanna a quick overview of implicit missing values? Check out vignette("implicit-na").

index_by() + summarise() to aggregate over calendar periods

index_by() is the counterpart of group_by() in temporal context, but it groups the index only. In conjunction with index_by(), summarise() and its scoped variants aggregate interested variables over calendar periods. index_by() goes hand in hand with the index functions including as.Date(), yearweek(), yearmonth(), and yearquarter(), as well as other friends from lubridate. For example, it would be of interest in computing average temperature and total precipitation per month, by applying yearmonth() to the index variable (referred to as .).

While collapsing rows (like summarise()), group_by() and index_by() will take care of updating the key and index respectively. This index_by() + summarise() combo can help with regularising a tsibble of irregular time space too.

Rolling with functional programming: slide(), tile(), stretch()

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

Rolling window animation

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

Looking for rolling in parallel? Their multiprocessing equivalents are prefixed with future_. More examples can be found at vignette("window").


Please note that this project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms.