mixtime

Flexible time vectors for R

GPL-3.0 License

Stars
3

output: github_document

knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.path = "man/figures/README-",
  out.width = "100%"
)

mixtime

mixtime provides flexible time classes for representing mixed temporal granularities and custom calendar structures.

The feature wishlist for this package includes:

  • Multiple temporal granularities (sub-daily, daily, weekly, monthly, quarterly, etc.)
  • Optional and custom temporal origins
  • Custom calendar structures (working days/hours, holiday effects, trading days)
  • Mixed temporal classes (daily and weekly granularities in same vector)

Stretch goals for the package include:

  • Representing temporal nesting (2020-01-05 is nested by 2020-01 is nested by 2020)
  • time_join() operation which respects temporal nesting

Installation

The development version can be installed from GitHub with:

# install.packages("devtools")
devtools::install_github("mitchelloharawild/mixtime")

Example

Creating mixtimes.

library(mixtime)
yearmonth(0:11) # By default, time classes have an origin
yearmonth(0:11) - yearmonth(0) # However some operations can produce mixtimes without origins

yearquarter(0:3)
yearquarter(0:3) - yearquarter(0)

# Different temporal granularities can be combined:
c(yearquarter(0:3), yearmonth(0:11))

Sequences of mixtimes.

seq(yearmonth(0), yearmonth(10), by = 1)

Integration with tsibble.

tsibble::tsibble(time = yearmonth(0:5), index = time)
tsibble::tsibble(time = c(year(0), yearquarter(0:3), yearmonth(0:5), Sys.Date()), index = time)

Change granularities by updating the calendar.

x <- yearmonth(0:11)
tsibble::tsibble(
  yearmonth = x,
  yearquarter = set_time_units(x, tu_quarter(1)),
  year = set_time_units(x, tu_year(1)),
  index = yearmonth
)