| linear_time_helpers | R Documentation |
Convenience functions for creating common linear time representations. These functions work with different calendar systems and adapt based on the input data's calendar.
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), ...)
data |
A vector of time points (e.g. base::Date, base::POSIXt) |
discrete |
If |
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 |
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.
A mixtime time vector containing an mt_linear vector with chronons matching the function used.
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)
For more complex time structures, use linear_time() or new_linear_time_fn()
to create custom representations with any combination of chronons and granules.
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
# 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)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.