duration() creates a vector of durations with a specified chronon.
Durations represent a fixed span of time measured in a given time granule
(e.g., 3 months, 5 days), without reference to a specific point in time.
Usage
duration(
data,
chronon = time_chronon(data),
discrete = NULL,
calendar = time_calendar(data)
)Arguments
- data
A time vector of duration magnitudes, or an existing
duration()vector to convert tochronongranules.- chronon
A time granule expression representing the chronon, evaluated in the context of
calendar. Use unquoted expressions likemonth(1L)orday(1L). Chronons from a specific calendar can also be used (e.g.cal_gregorian$month(1L)). Defaults to the time chronon of the inputdata(time_chronon(data)).- discrete
Logical. If
TRUE(default), returns integer durations always rounding down (discrete time model). IfFALSE, returns fractional durations (continuous time model).- calendar
Calendar system used to evaluate
chronon. Defaults totime_calendar(data)for existing time objects. Common options include cal_gregorian and cal_isoweek.
See also
new_duration_fn()for creating reusable duration functionscal_gregorian, cal_isoweek for calendar systems
Examples
# A duration of 3 months
duration(3L, cal_gregorian$month(1L))
#> <mixtime[1]>
#> [1] 3 months
# A vector of durations in days
duration(1:7, cal_gregorian$day(1L))
#> <mixtime[7]>
#> [1] 1 day 2 days 3 days 4 days 5 days 6 days 7 days
# Convert a duration of 4 days into weeks
duration(days(4), cal_isoweek$week(1L), discrete = FALSE)
#> <mixtime[1]>
#> [1] 0.5714286 weeks
duration(days(4), cal_isoweek$week(1L), discrete = TRUE)
#> <mixtime[1]>
#> [1] 0 weeks