linear_time_helpers: Linear time helper functions

linear_time_helpersR Documentation

Linear time helper functions

Description

Convenience functions for creating common linear time representations. These functions work with different calendar systems and adapt based on the input data's calendar.

Usage

year(data, discrete = TRUE, calendar = time_calendar(data), ...)

yearquarter(data, discrete = TRUE, calendar = time_calendar(data), ...)

yearmonth(data, discrete = TRUE, calendar = time_calendar(data), ...)

yearweek(data, discrete = TRUE, calendar = time_calendar(data), ...)

date(data, discrete = TRUE, calendar = time_calendar(data), ...)

datetime(data, discrete = TRUE, calendar = time_calendar(data), ...)

Arguments

data

A vector of time points (e.g. base::Date, base::POSIXt)

discrete

If TRUE, the number of chronons since Unix epoch that data falls into is returned as an integer. If FALSE, a fractional number of chronons is returned (analagous to time using a continuous time model).

calendar

A calendar used to evaluate the time units. Defaults to the calendar of the input data. Common options include cal_gregorian and cal_isoweek.

...

Additional arguments for linear_time(), such as tz for timezones.

Details

These functions create linear time representations with different chronons and granules:

  • year(): Represents time in whole years. The chronon is one year.

  • yearquarter(): Represents time in quarters, grouped by year. The chronon is one quarter, with years as the granule.

  • yearmonth(): Represents time in months, grouped by year. The chronon is one month, with years as the granule.

  • yearweek(): Represents time in weeks, grouped by year. The chronon is one week, with years as the granule. Defaults to ISO 8601 week calendar.

Value

A mixtime time vector containing an mt_linear vector with chronons matching the function used.

Calendar flexibility

These functions adapt to the calendar system of the input data. For example:

  • year(date("2025-12-29")) returns a Gregorian year

  • year(yearweek(date("2025-12-29"))) returns an ISO week-based year

You can also explicitly specify a calendar using the calendar argument:

year(date("2025-12-29"), calendar = cal_isoweek)

Custom linear time representations

For more complex time structures, use linear_time() or new_linear_time_fn() to create custom representations with any combination of chronons and granules.

See Also

  • linear_time() for creating custom linear time representations

  • new_linear_time_fn() for creating reusable linear time functions

  • cal_gregorian, cal_isoweek for calendar systems

Examples


# Gregorian year
year(Sys.Date())
year(Sys.Date(), discrete = FALSE)

# ISO week-based year
year(yearweek(Sys.Date()))

# Year-quarter
yearquarter(Sys.Date())
yearquarter(Sys.Date(), discrete = FALSE)

# Year-month
yearmonth(Sys.Date())
yearmonth(Sys.Date(), discrete = FALSE)

# Year-week (ISO 8601)
yearweek(Sys.Date())
yearweek(0:52)


mixtime documentation built on Nov. 18, 2026, 5:08 p.m.