time_interval: S3-based Time Intervals (Currently very experimental and so...

View source: R/time_interval.R

time_intervalR Documentation

S3-based Time Intervals (Currently very experimental and so subject to change)

Description

Inspired by both 'lubridate' and 'ivs', time_interval objects are lightweight S3 objects of a fixed width. This enables fast and flexible representation of time data such as months, weeks, and more. They are all left closed, right open intervals.

Usage

time_interval(start = integer(), width = resolution(start))

is_time_interval(x)

new_time_interval(start, width)

interval_start(x)

interval_end(x)

interval_width(x)

interval_count(x)

interval_range(x)

Arguments

start

Start time.
E.g a Date, POSIXt, numeric and more.

width

Interval width supplied as a timespan. By default this is the resolution of a time vector so for example, a date's resolution is exactly 1 day, therefore time_interval(Sys.Date()) simply represents today's date as an interval.

x

A time_interval.

Details

Currently because of limitations with the S3/S4 system, one can't use time intervals directly with lubridate periods. To navigate around this, timeplyr::timespan() can be used. e.g. instead of interval / weeks(3), use interval / timespan(weeks(3)) or even interval / "3 weeks". where interval is a time_interval.

To perform interval algebra it is advised to use the 'ivs' package. To convert a time_interval into an ivs_iv, use ivs::iv(interval_start(x), interval_end(x)).

Value

An object of class time_interval.
is_time_interval returns a logical of length 1.
interval_start returns the start times.
interval_end returns the end times.
interval_width returns the width of the interval as a timespan.
interval_count returns a data frame of unique intervals and their counts.
interval_range returns a the range of the interval.
new_time_interval is a bare-bones version of time_interval() that performs no checks.

See Also

interval_start

Examples

library(dplyr)
library(timeplyr)
library(lubridate)
x <- 1:10
int <- time_interval(x, 100)
int

month_start <- floor_date(today(), unit = "months")
month_int <- time_interval(month_start, "month")
month_int

interval_start(month_int)
interval_end(month_int)

# Divide an interval into different time units
time_interval(today(), years(10)) / timespan("year")

# Cutting Sepal Length into blocks of width 1
int <- time_cut_width(iris$Sepal.Length, 1)
interval_count(int)


timeplyr documentation built on April 3, 2025, 6:15 p.m.