This function takes the open_hours variable in an already-read Core file (stored as a data.table) and expands it to seven list-type columns, where the elements of the list in each row are a set of vectors for opening/closing times, in military time format (1:30PM = 13.5). So an observation of c(8,10,12,14) would be a business that opens at 8, closes at 10, opens again at noon, and closes again at 2PM on that day. Options are available to produce long instead of wide expansions as well.

expand_open_hours(
  dt,
  format = c("wide", "long", "long-expand", "long_expand"),
  open_hours = "open_hours",
  colnames = NULL,
  drop_missing = FALSE,
  convert_hour = TRUE,
  days = c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday",
    "Sunday")
)

Arguments

dt

A data.table containing the open_hours column (or an object that can be coerced to a data.table).

format

Can be 'wide' (seven new list-columns, one for each day), 'long' (turn each row into seven rows, then two new columns: one for day-of-week and one list-column with opening/closing times), or 'long-expand'/'long_expand' ('long' but then also break the single list-column out into a set of numeric start/end columns). Note that for 'long-expand', many locations have more than one set of open/close hours per day, so there will be more than one open/close column each.

open_hours

A character variable with the name of the open_hours column.

colnames

For format = 'wide', the name stub for the column names, by default 'open_hours' to get 'open_hoursSunday', 'open_hoursMonday', etc.. For format='long', a two-element vector (by default c('weekday','open_hours')) with the name of the column indicating the day, and the list-column with the open hours information in it. For format = 'long-expand', a three-element vector with the weekday column, the name stub for "opening hour" and the name stub for "closing hour" (with numbers 1, 2, 3, etc. appended afterwards), by default c('weekday','opens','closes').

drop_missing

Drop any rows with a missing open_hours observation.

convert_hour

Convert hour strings like '15:30' to numbers like 15.5. This does slow down the function.

days

A character vector of the days to keep. Cutting down here can save some time/memory especially if you are not going format = 'wide'.

Details

Returns the same data.table but with the new columns/rows added. May change the order of the data.