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 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)
#> 01/Aug — 07/Aug
#> 08/Aug — 14/Aug
#> 15/Aug — 21/Aug
#> 22/Aug — 28/Aug
#> 29/Aug — 04/Sep
#> 05/Sep — 11/Sep
#> 12/Sep — 18/Sep
#> 19/Sep — 25/Sep
#> 26/Sep — 02/Oct
#> 03/Oct — 09/Oct
#> 10/Oct — 16/Oct
labels(eg, ifmt="{start}", dfmt="%d/%b/%y")
#> 01/Aug/24
#> 08/Aug/24
#> 15/Aug/24
#> 22/Aug/24
#> 29/Aug/24
#> 05/Sep/24
#> 12/Sep/24
#> 19/Sep/24
#> 26/Sep/24
#> 03/Oct/24
#> 10/Oct/24
labels(eg, ifmt="until {end}", dfmt="%d %b %Y")
#> until 07 Aug 2024
#> until 14 Aug 2024
#> until 21 Aug 2024
#> until 28 Aug 2024
#> until 04 Sep 2024
#> until 11 Sep 2024
#> until 18 Sep 2024
#> until 25 Sep 2024
#> until 02 Oct 2024
#> until 09 Oct 2024
#> until 16 Oct 2024

# 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"))
#> No unit given. Guessing a sensible value from the dates gives: 7d 0H 0M 0S
#>  [1] "week 0"  "week 1"  "week 2"  "week 3"  "week 4"  "week 5"  "week 6" 
#>  [8] "week 7"  "week 8"  "week 9"  "week 10"