Skip to contents

Create a set of labels for a time period based on the start and duration of the period. The format is configurable using the start and end dates and the dfmt and ifmt parameters, however if the time period has names then these are used in preference.

Usage

# S3 method for class 'time_period'
labels(
  object,
  ...,
  dfmt = "%d/%b",
  ifmt = "{start} — {end}",
  na.value = "Unknown"
)

Arguments

object

a set of decimal times as a time_period

...

not used

dfmt

a strptime format specification for the format of the date

ifmt

a glue spec referring to start and end of the period as a formatted date

na.value

a label for NA times

Value

a set of character labels for the time

Examples

eg = as.time_period(Sys.Date()+0:10*7, anchor="start")
#> No unit given. Guessing a sensible value from the dates gives: 7d 0H 0M 0S
labels(eg)
#> 24/Jun — 30/Jun
#> 01/Jul — 07/Jul
#> 08/Jul — 14/Jul
#> 15/Jul — 21/Jul
#> 22/Jul — 28/Jul
#> 29/Jul — 04/Aug
#> 05/Aug — 11/Aug
#> 12/Aug — 18/Aug
#> 19/Aug — 25/Aug
#> 26/Aug — 01/Sep
#> 02/Sep — 08/Sep
labels(eg, ifmt="{start}", dfmt="%d/%b/%y")
#> 24/Jun/25
#> 01/Jul/25
#> 08/Jul/25
#> 15/Jul/25
#> 22/Jul/25
#> 29/Jul/25
#> 05/Aug/25
#> 12/Aug/25
#> 19/Aug/25
#> 26/Aug/25
#> 02/Sep/25
labels(eg, ifmt="until {end}", dfmt="%d %b %Y")
#> until 30 Jun 2025
#> until 07 Jul 2025
#> until 14 Jul 2025
#> until 21 Jul 2025
#> until 28 Jul 2025
#> until 04 Aug 2025
#> until 11 Aug 2025
#> until 18 Aug 2025
#> until 25 Aug 2025
#> until 01 Sep 2025
#> until 08 Sep 2025

# labels retained in constructor:
eg2 = Sys.Date()+0:10*7
names(eg2) = paste0("week ",0:10)
labels(eg2)
#>  [1] "week 0"  "week 1"  "week 2"  "week 3"  "week 4"  "week 5"  "week 6" 
#>  [8] "week 7"  "week 8"  "week 9"  "week 10"
labels(as.time_period(eg2, anchor="start"))
#>  [1] "week 0"  "week 1"  "week 2"  "week 3"  "week 4"  "week 5"  "week 6" 
#>  [8] "week 7"  "week 8"  "week 9"  "week 10"