Skip to contents

Utility functions for accessing the start (end) dates for each element of a grates object and also checking whether a date is contained within that range

Usage

date_start(x)

date_end(x)

date %during% x

Arguments

x

grouped date vector.

date

A scalar <date> object.

Value

For date_start and date_end The requested start (end) dates for each element in the input. For %during% a logical vector indicating whether the date was present within the range of the tested object.

Examples

dates <- as.Date("2020-01-01") + 1:14

week <- as_isoweek(dates)
date_start(week)
#>  [1] "2019-12-30" "2019-12-30" "2019-12-30" "2019-12-30" "2020-01-06"
#>  [6] "2020-01-06" "2020-01-06" "2020-01-06" "2020-01-06" "2020-01-06"
#> [11] "2020-01-06" "2020-01-13" "2020-01-13" "2020-01-13"
date_end(week)
#>  [1] "2020-01-05" "2020-01-05" "2020-01-05" "2020-01-05" "2020-01-12"
#>  [6] "2020-01-12" "2020-01-12" "2020-01-12" "2020-01-12" "2020-01-12"
#> [11] "2020-01-12" "2020-01-19" "2020-01-19" "2020-01-19"
dates[1L] %during% week
#>  [1]  TRUE  TRUE  TRUE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
#> [13] FALSE FALSE

period <- as_period(dates, n = 3)
date_start(period)
#>  [1] "2019-12-31" "2020-01-03" "2020-01-03" "2020-01-03" "2020-01-06"
#>  [6] "2020-01-06" "2020-01-06" "2020-01-09" "2020-01-09" "2020-01-09"
#> [11] "2020-01-12" "2020-01-12" "2020-01-12" "2020-01-15"
date_end(period)
#>  [1] "2020-01-02" "2020-01-05" "2020-01-05" "2020-01-05" "2020-01-08"
#>  [6] "2020-01-08" "2020-01-08" "2020-01-11" "2020-01-11" "2020-01-11"
#> [11] "2020-01-14" "2020-01-14" "2020-01-14" "2020-01-17"
dates[14L] %during% period
#>  [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
#> [13] FALSE  TRUE