We need to map "dates" onto dust::dust's concept of model "step" and we do this by mapping a date such as 2020-03-02 into the number of days into 2020 (62 here, with the 1st of January being day 1). We call this integer number a "sircovid date".

sircovid_date(date)

sircovid_date_as_date(date)

as_sircovid_date(date)

as_date(date)

Arguments

date

A Date object, or something that can be converted to one, or a "sircovid date"; see Details

Value

An integer, being the number of days into 2020

Details

There are several related functions here

  • sircovid_date converts its argument into an R Date object, then applies this tranformation. If the argument is not a Date object or a string representing one, an error will be thrown.

  • sircovid_date_to_date does the reverse conversion to sircovid_date, converting an integer sircovid date into an R Date

  • as_sircovid_date does the same conversion as sircovid_date but will assume that an integer already represents a sircovid date and will return it unmodified rather than erroring.

  • as_date does a string to date conversion, using as.Date() but requiring the dates are in ISO 8601 (YYYY-MM-DD) format (it is a helper that avoids conversion to NA, instead throwing an error)

Examples

# Convert dates into sircovid dates:
sircovid::sircovid_date("2020-01-01")
#> [1] 1
sircovid::sircovid_date(c("2020-03-01", "2020-10-01"))
#> [1]  61 275

# Reverse the conversion:
sircovid::sircovid_date_as_date(1)
#> [1] "2020-01-01"
sircovid::sircovid_date_as_date(c(61, 275))
#> [1] "2020-03-01" "2020-10-01"

# Double conversion not possible with sircovid_date...
try(sircovid::sircovid_date(61))
#> Error in as_date(date) : Expected ISO dates or R dates - please convert
# ...but allowed with as_sircovid_date
sircovid::as_sircovid_date(61)
#> [1] 61

# Strict date conversion with as_date
sircovid::as_date("2020-03-01")
#> [1] "2020-03-01"
try(sircovid::as_date("03-01-2020"))
#> Error in sircovid::as_date("03-01-2020") : 
#>   Expected ISO dates or R dates - please convert