Skip to content

Instantly share code, notes, and snippets.

@mgei
Created February 21, 2019 10:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mgei/5708f7e605d5d23a45777a1d156b0ee8 to your computer and use it in GitHub Desktop.
Save mgei/5708f7e605d5d23a45777a1d156b0ee8 to your computer and use it in GitHub Desktop.
R create tibble for open close hours to be used for timevis with type = "range"
library(tidyverse)
library(lubridate)
days <- tibble(date = seq(date("2018-01-01"), by = "days", length.out = 730)) %>%
mutate(day = weekdays(date))
hours <- tibble(daytype = c("weekday", "weekday", "weekday", "weekday", "weekday", "weekend"),
time_from = c("00:00", "08:00", "12:00", "13:00", "17:00", "00:00" ),
time_to = c("08:00", "12:00", "13:00", "17:00", "23:59", "23:59" ),
state = c("closed", "open", "closed", "open", "closed", "closed"))
week <- tibble(#day = c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"),
day = c("Montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag", "Samstag", "Sonntag"),
daytype = c(rep("weekday", 5), rep("weekend", 2)))
days %>% left_join(left_join(week, hours, by = "daytype"), by = "day") %>%
mutate(start = ymd_hm(paste(date, time_from)),
end = ymd_hm(paste(date, time_to))) %>%
select(start, end, state) -> openclosed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment