Skip to contents

Time periods are just a zero based numeric representation of dates with a time unit baked in. This allows variable length periods (e.g. days or weeks), and fractional days to be represented in a consistent(ish) way

Usage

as.time_period(x, unit = NULL, start_date = NULL, anchor = NULL, ...)

# S3 method for time_period
c(..., recursive = F)

# S3 method for time_period
[(x, ...)

# S3 method for time_period
[(x, ...) <- value

# S3 method for time_period
[[(x, ...)

# S3 method for time_period
[[(x, ...) <- value

is.time_period(x)

# S3 method for time_period
print(x, ...)

Arguments

x

a vector of numbers (may be integer or real) or a time_period

unit

the length of one unit of time. This will be either a integer number of days, or a specification such as "1 week", or another time_period. If x is a time_period, and the unit is different then from that of x this will return a new time_period using the new units.

start_date

the zero time date as something that can be coerced to a date. If the x input is already a time_period and this is different to its start_date then it will be recalibrated to use the new start date.

anchor

only relevant is x is a vector of dates and start_date is not specified, this is a date, or "start" or "end" or a weekday name e.g. "mon". With the vector of dates in x it will find a reference date for the time-series. If this is NULL and start_date is also NULL it will fall back to getOption("day_zero","2019-12-29")

...

used for subtype implementations

recursive

concatenate recursively

value

the value

Value

a time_period class, consisting of a vector of numbers, with attributes for time period and start_date

Functions

  • c(time_period): Combine time_period

  • [: Subset a time_period

  • `[`(time_period) <- value: Assign values to a subset of a time_period

  • [[: Get a value in a time_period

  • `[[`(time_period) <- value: Assign a value in a time_period

  • is.time_period(): Check is a time_period

  • print(time_period): Print a time_period

Examples

# 100 weeks from 2020-01-01

tmp = as.time_period(0:100, 7, "2020-01-01")
as.Date(tmp)
#>   [1] "2020-01-01" "2020-01-08" "2020-01-15" "2020-01-22" "2020-01-29"
#>   [6] "2020-02-05" "2020-02-12" "2020-02-19" "2020-02-26" "2020-03-04"
#>  [11] "2020-03-11" "2020-03-18" "2020-03-25" "2020-04-01" "2020-04-08"
#>  [16] "2020-04-15" "2020-04-22" "2020-04-29" "2020-05-06" "2020-05-13"
#>  [21] "2020-05-20" "2020-05-27" "2020-06-03" "2020-06-10" "2020-06-17"
#>  [26] "2020-06-24" "2020-07-01" "2020-07-08" "2020-07-15" "2020-07-22"
#>  [31] "2020-07-29" "2020-08-05" "2020-08-12" "2020-08-19" "2020-08-26"
#>  [36] "2020-09-02" "2020-09-09" "2020-09-16" "2020-09-23" "2020-09-30"
#>  [41] "2020-10-07" "2020-10-14" "2020-10-21" "2020-10-28" "2020-11-04"
#>  [46] "2020-11-11" "2020-11-18" "2020-11-25" "2020-12-02" "2020-12-09"
#>  [51] "2020-12-16" "2020-12-23" "2020-12-30" "2021-01-06" "2021-01-13"
#>  [56] "2021-01-20" "2021-01-27" "2021-02-03" "2021-02-10" "2021-02-17"
#>  [61] "2021-02-24" "2021-03-03" "2021-03-10" "2021-03-17" "2021-03-24"
#>  [66] "2021-03-31" "2021-04-07" "2021-04-14" "2021-04-21" "2021-04-28"
#>  [71] "2021-05-05" "2021-05-12" "2021-05-19" "2021-05-26" "2021-06-02"
#>  [76] "2021-06-09" "2021-06-16" "2021-06-23" "2021-06-30" "2021-07-07"
#>  [81] "2021-07-14" "2021-07-21" "2021-07-28" "2021-08-04" "2021-08-11"
#>  [86] "2021-08-18" "2021-08-25" "2021-09-01" "2021-09-08" "2021-09-15"
#>  [91] "2021-09-22" "2021-09-29" "2021-10-06" "2021-10-13" "2021-10-20"
#>  [96] "2021-10-27" "2021-11-03" "2021-11-10" "2021-11-17" "2021-11-24"
#> [101] "2021-12-01"

range(tmp)
#> time unit: week, origin: 2020-01-01 (a Wednesday)
#> [1]   0 100
min(tmp)
#> time unit: week, origin: 2020-01-01 (a Wednesday)
#> [1] 0
tmp2 = as.integer(as.Date(tmp))
# testthat::expect_true(all(na.omit(tmp2-lag(tmp2)) == 7))

tmp2 = as.time_period(0:23, 1/24, "2020-01-01")
as.POSIXct(tmp2)
#>  [1] "2020-01-01 00:00:00 GMT" "2020-01-01 01:00:00 GMT"
#>  [3] "2020-01-01 02:00:00 GMT" "2020-01-01 03:00:00 GMT"
#>  [5] "2020-01-01 04:00:00 GMT" "2020-01-01 05:00:00 GMT"
#>  [7] "2020-01-01 06:00:00 GMT" "2020-01-01 07:00:00 GMT"
#>  [9] "2020-01-01 08:00:00 GMT" "2020-01-01 09:00:00 GMT"
#> [11] "2020-01-01 10:00:00 GMT" "2020-01-01 11:00:00 GMT"
#> [13] "2020-01-01 12:00:00 GMT" "2020-01-01 13:00:00 GMT"
#> [15] "2020-01-01 14:00:00 GMT" "2020-01-01 15:00:00 GMT"
#> [17] "2020-01-01 16:00:00 GMT" "2020-01-01 17:00:00 GMT"
#> [19] "2020-01-01 18:00:00 GMT" "2020-01-01 19:00:00 GMT"
#> [21] "2020-01-01 20:00:00 GMT" "2020-01-01 21:00:00 GMT"
#> [23] "2020-01-01 22:00:00 GMT" "2020-01-01 23:00:00 GMT"

# convert timeseries to new "unit"
tmp = as.time_period(0:100, 7, "2020-01-01")
tmp2 = as.time_period(tmp,1)
testthat::expect_equal(as.numeric(tmp2), 0:100*7)