Arithmetic for underspecified cyclical time (mt_cyclical_free)
Source: R/cyclical_free_ops.R
mt_cyclical_free-arithmetic.RdUnderspecified cyclical time (see cyclical_time()) is a position within a
cycle whose enclosing cycle is unknown, e.g. month_of_year(3) is
"March of some year". Only operations that don't depend on the enclosing
cycle are defined:
Differences:
a - bbetween values of the same cycle is aduration()in their common chronon, the difference between their positions within the cycle (month_of_year(3) - month_of_year(1)is 2 months, andmonth_of_year(1) - month_of_year(3)is -2 months). This follows the linear ordering of positions within one cycle (see mt_cyclical_free-compare), for cycles of fixed and variable length alike (day_of_month(31) - day_of_month(1)is 30 days).Shifts:
x + nandx - nshift bynchronons, wherenis a number or aduration()that is a whole number of the value's chronons with a fixed ratio (e.g.month_of_year(1) + months(2)ortime_of_day(3600) + hours(2)). Shifts wrap around cycles of a fixed length (month_of_year(12) + 1is January,day_of_week(1) - 1is Sunday), soa + (b - a) == b. Cycles of variable length (e.g.day_of_month(),day_of_year()andweek_of_year()) can't be shifted, whatever the values:day_of_month(28) + 1is day 29, or day 1 in a non-leap February.seq()follows the same rules as shifts. The sequence runs linearly within one cycle betweenfromandto(soseq(month_of_year(11), month_of_year(2))counts down from November to February), while sequences of a givenlength.outwrap around the cycle.
Everything else is an error: shifting by a duration that isn't a whole
number of chronons with a fixed ratio (month_of_year(1) + days(1)), or
that spans a whole cycle or more (month_of_year(1) + years(1) would move
to another, unknown, year); differences between values of differing
cycles, or between an underspecified value and a linear or located time
point; subtracting an underspecified value from a number or duration; multiplying or
dividing; and summaries that create new values, such as mean() or
cumsum(). Comparisons and ordering (sort(), min(), max(),
range()) are defined, see mt_cyclical_free-compare.
To do any of these, locate the value in a specific enclosing cycle (e.g. a
particular month for a day of month) with time_compose(), giving linear
time, e.g.
time_compose(yearmonth("2024-02"), day_of_month(28)) + days(1).
Missing values stay missing.
Usage
# S4 method for class 'mt_cyclical_free'
e1 + e2
# S4 method for class 'mt_cyclical_free'
e1 - e2Value
For differences, a duration (mt_duration). For shifts, a
mt_cyclical_free vector of the same type as the underspecified operand.
See also
mt_time-arithmetic for arithmetic on linear and located cyclical time points.
Examples
month_of_year(12) + 1L
#> <mixtime[1]>
#> [1] Jan
month_of_year(1) - months(1)
#> <mixtime[1]>
#> [1] Dec
day_of_week(6) + 0:3
#> <mixtime[4]>
#> [1] Sat Sun Mon Tue
month_of_year(3) - month_of_year(1)
#> <mixtime[1]>
#> [1] 2 months
day_of_month(31) - day_of_month(1)
#> <mixtime[1]>
#> [1] 30 days
seq(day_of_week(6), by = 1, length.out = 4)
#> <mixtime[4]>
#> [1] Sat Sun Mon Tue
# Cycles of variable length can't be shifted until located
try(day_of_month(28) + 1L)
#> Error : Can't add 1 day to a day of month in an unknown month.
#> ✖ The number of days in a month varies, so where a day of month lands after
#> adding 1 day depends on which month it is in.
#> ℹ Locate it in a specific month first, e.g. `time_compose(yearmonth("2024-02"),
#> x) + days(1)`.
time_compose(yearmonth("2024-02"), day_of_month(28)) + days(1)
#> <mixtime[1]>
#> [1] 2024-02-29