Skip to contents

Adds a timeslice column named after the calendar (its meta$name), plus optionally the calendar's timeframe columns, their token labels and the four length columns, all prefixed "<name>.". A timeframe column holds the member of the row at that timeframe (m01_h00 at HOUR); tokens = TRUE adds the token label (h00) beside it, which is what an hour-of-day or month axis needs. Because every calendar attaches under its own name, several calendars can be joined to the same dataset – and a dataset carrying two label columns is a direct crosswalk between those calendars.

Usage

join_calendar(
  data,
  calendar,
  key = NULL,
  timeframes = NULL,
  meta = FALSE,
  as_factor = TRUE,
  year = NULL,
  by = NULL,
  tz = "UTC",
  collect = NULL,
  tokens = FALSE
)

Arguments

data

The dataset, in any supported backend (see recast_calendar()'s Backends section).

calendar

A named Calendar.

key

Key column of data: a timeslice-label column, or a POSIXct datetime column. NULL (default) auto-detects as described above.

timeframes

Character vector of the calendar's timeframes to attach as "<name>.<TIMEFRAME>" columns (default: none). TRUE attaches all of them. The columns hold members.

meta

Attach the length columns of the leaftable as "<name>.share", "<name>.width", "<name>.weight" and "<name>.total" (default FALSE). The part of the year a timeslice stands for is "<name>.total" (see Calendar).

as_factor

Attach timeframe columns as vocabulary-ordered factors (default TRUE) or plain character. (Lazy backends store them as dictionary/character columns.)

year

Model year(s) for the base grid when attaching by datetime. Default: the span of years observed in the data, padded one year each side.

by, tz

Base-grid resolution and time zone for the datetime route, as in expand_calendar().

collect

For lazy inputs: materialise (TRUE) or return the query (default).

tokens

Also attach the token label of each attached timeframe as "<name>.token.<TIMEFRAME>" (default FALSE): h00 for the member m01_h00. With as_factor, an ordered factor in vocabulary order.

Value

data with the new column(s) appended, in the input's class (lazy in, lazy out).

Details

The key is auto-detected: an existing column named like the calendar is used as-is; else a timeslice column (labels validated against the calendar, with a warning for unknown codes); else a datetime column (labels computed on the base grid via datetime_to_timeslice() – this is how a calendar is attached to raw datetime observations). Existing columns are never overwritten; the join errors instead.

Examples

cal <- calendar("m12_h24")
x <- data.frame(timeslice = calendar_timeslices(cal), load = 1)
head(join_calendar(x, cal))                    # adds `m12_h24`
#>   timeslice load m12_h24
#> 1   m01_h00    1 m01_h00
#> 2   m01_h01    1 m01_h01
#> 3   m01_h02    1 m01_h02
#> 4   m01_h03    1 m01_h03
#> 5   m01_h04    1 m01_h04
#> 6   m01_h05    1 m01_h05
head(join_calendar(x, cal, timeframes = TRUE)) # + m12_h24.MONTH, ...
#>   timeslice load m12_h24 m12_h24.MONTH m12_h24.HOUR
#> 1   m01_h00    1 m01_h00           m01      m01_h00
#> 2   m01_h01    1 m01_h01           m01      m01_h01
#> 3   m01_h02    1 m01_h02           m01      m01_h02
#> 4   m01_h03    1 m01_h03           m01      m01_h03
#> 5   m01_h04    1 m01_h04           m01      m01_h04
#> 6   m01_h05    1 m01_h05           m01      m01_h05
# ... and the token labels: m12_h24.token.HOUR is the hour of the day
head(join_calendar(x, cal, timeframes = "HOUR", tokens = TRUE))
#>   timeslice load m12_h24 m12_h24.HOUR m12_h24.token.HOUR
#> 1   m01_h00    1 m01_h00      m01_h00                h00
#> 2   m01_h01    1 m01_h01      m01_h01                h01
#> 3   m01_h02    1 m01_h02      m01_h02                h02
#> 4   m01_h03    1 m01_h03      m01_h03                h03
#> 5   m01_h04    1 m01_h04      m01_h04                h04
#> 6   m01_h05    1 m01_h05      m01_h05                h05

# two calendars on one dataset = a direct crosswalk between them
xt <- data.frame(datetime = seq(as.POSIXct("2021-01-01", tz = "UTC"),
                                by = "hour", length.out = 48), v = 1)
xt <- join_calendar(xt, calendar("m12_h24"))
xt <- join_calendar(xt, calendar("q4_h24"))
head(xt)
#>              datetime v m12_h24 q4_h24
#> 1 2021-01-01 00:00:00 1 m01_h00 Q1_h00
#> 2 2021-01-01 01:00:00 1 m01_h01 Q1_h01
#> 3 2021-01-01 02:00:00 1 m01_h02 Q1_h02
#> 4 2021-01-01 03:00:00 1 m01_h03 Q1_h03
#> 5 2021-01-01 04:00:00 1 m01_h04 Q1_h04
#> 6 2021-01-01 05:00:00 1 m01_h05 Q1_h05