Skip to content

Instantly share code, notes, and snippets.

@ozjimbob
Created October 2, 2018 00:13
Show Gist options
  • Save ozjimbob/52634e558973baf272af917b55d2ea73 to your computer and use it in GitHub Desktop.
Save ozjimbob/52634e558973baf272af917b55d2ea73 to your computer and use it in GitHub Desktop.
library("reticulate")
library(tidyverse)
library(purrr)
# We use the python "holidays" package for the basic holidays
# Installed it using pip first
holidays <- import("holidays")
tas_holidays <- holidays$Australia(prov="TAS")
# List of all dates from 2000 to 2018
date_list = as.Date(seq(as.Date("2000-01-01"),as.Date("2018-12-31"),1))
out = tibble(date = as.character(date_list))
# Wrapper for the python function, because it returns NULL if there is no holiday
gdate <- function(date){
o=tas_holidays$get(date)
if(is.null(o)){o=""}
o
}
# Run those basic holidays over the dates
out2 = map_chr(out$date,gdate)
out$holiday = out2
# Now the fun starts.
# Devonport cup
# Wednesday between 5th and 11th Jan
for(year in 2000:2018){
date_seq = seq(as.Date(paste0(year,"-01-05")),as.Date(paste0(year,"-01-11")),1)
wd = format(date_seq,"%a")
d = which(wd == "Wed")
wd = date_seq[d]
wd = as.character(wd)
out$holiday[out$date==wd]="Devonport Cup"
}
# Royal Hobart Regatta
# Second monday in February
for(year in 2000:2018){
date_seq = seq(as.Date(paste0(year,"-01-01")),as.Date(paste0(year,"-12-31")),1)
date_seq = date_seq[format(date_seq,"%m")=="02"]
wd = format(date_seq,"%a")
d = which(wd == "Mon")
wd = date_seq[d[2]]
wd = as.character(wd)
out$holiday[out$date==wd]="Royal Hobart Regatta"
}
# Launceston Cup
# Last Wednesday in February
for(year in 2000:2018){
date_seq = seq(as.Date(paste0(year,"-01-01")),as.Date(paste0(year,"-12-31")),1)
date_seq = date_seq[format(date_seq,"%m")=="02"]
wd = format(date_seq,"%a")
d = which(wd == "Wed")
wd = date_seq[tail(d,1)]
wd = as.character(wd)
out$holiday[out$date==wd]="Launceston Cup"
}
# King Island Show
# First Tuesday in March
for(year in 2000:2018){
date_seq = seq(as.Date(paste0(year,"-01-01")),as.Date(paste0(year,"-12-31")),1)
date_seq = date_seq[format(date_seq,"%m")=="03"]
wd = format(date_seq,"%a")
d = which(wd == "Tue")
wd = date_seq[d[1]]
wd = as.character(wd)
out$holiday[out$date==wd]="King Island Show"
}
# AGFEST
# Friday following first Thursday in May
for(year in 2000:2018){
date_seq = seq(as.Date(paste0(year,"-01-01")),as.Date(paste0(year,"-12-31")),1)
date_seq = date_seq[format(date_seq,"%m")=="05"]
wd = format(date_seq,"%a")
d = which(wd == "Thu")
wd = date_seq[d[1]+1]
wd = as.character(wd)
out$holiday[out$date==wd]="AGFEST"
}
# Burnie Show
# Friday before first saturday in October
for(year in 2000:2018){
date_seq = seq(as.Date(paste0(year,"-01-01")),as.Date(paste0(year,"-12-31")),1)
date_seq = date_seq[format(date_seq,"%m")=="10"]
wd = format(date_seq,"%a")
d = which(wd == "Sat")
sel_date = d[1]-1
if(sel_date > 0){
wd = date_seq[d[1]-1]
wd = as.character(wd)
}else{
wd = paste0(year,"-09-30")
}
out$holiday[out$date==wd]="Burnie Show"
}
# Royal Launceston Show
# Thursday Before the second Saturday in October
for(year in 2000:2018){
date_seq = seq(as.Date(paste0(year,"-01-01")),as.Date(paste0(year,"-12-31")),1)
date_seq = date_seq[format(date_seq,"%m")=="10"]
wd = format(date_seq,"%a")
d = which(wd == "Sat")
wd = date_seq[d[2]-2]
wd = as.character(wd)
out$holiday[out$date==wd]="Royal Launceston Show"
}
# Flinders Island Show
# Friday Before the third Saturday in October
for(year in 2000:2018){
date_seq = seq(as.Date(paste0(year,"-01-01")),as.Date(paste0(year,"-12-31")),1)
date_seq = date_seq[format(date_seq,"%m")=="10"]
wd = format(date_seq,"%a")
d = which(wd == "Sat")
wd = date_seq[d[3]-1]
wd = as.character(wd)
out$holiday[out$date==wd]="Flinders Island Show"
}
# Royal Hobart Show
# Thursday Before the fourth Saturday in October
for(year in 2000:2018){
date_seq = seq(as.Date(paste0(year,"-01-01")),as.Date(paste0(year,"-12-31")),1)
date_seq = date_seq[format(date_seq,"%m")=="10"]
wd = format(date_seq,"%a")
d = which(wd == "Sat")
wd = date_seq[d[4]-2]
wd = as.character(wd)
out$holiday[out$date==wd]="Royal Hobart Show"
}
# Recreation Day
# First monday in November
for(year in 2000:2018){
date_seq = seq(as.Date(paste0(year,"-01-01")),as.Date(paste0(year,"-12-31")),1)
date_seq = date_seq[format(date_seq,"%m")=="11"]
wd = format(date_seq,"%a")
d = which(wd == "Mon")
wd = date_seq[d[1]]
wd = as.character(wd)
out$holiday[out$date==wd]="Recreation Day"
}
# Devonport Show
# First monday in November
for(year in 2000:2018){
date_seq = seq(as.Date(paste0(year,"-11-25")),as.Date(paste0(year,"-12-01")),1)
wd = format(date_seq,"%a")
d = which(wd == "Fri")
wd = date_seq[d[1]]
wd = as.character(wd)
out$holiday[out$date==wd]="Devonport Show"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment