How to generate dummies for all holidays (date format = "%M/%d/%Y" ) for all years?

Hello,

I am trying to generate a dummy variable Ht that takes on the value 1 if that date (format = "%Y/%M/%D") is a national holiday. My dataset goes from 1979-07-03 to 2018-12-31, therefore I need to for example generate a dummy that = 1 on Jan 1st, December 31st...etc for all the years. (Same dates every single year across all years).

I can't seem to find codes to get this dummy. Can someone please help?

Just to confirm; you are only interested in holidays that occur on the same calendar date each year? You don't care about "movable feasts" like Easter, Ramadan and Passover in the religious realm or "fourth Thursday in November" in the secular?

There are many ways to do this, this simple example would be one of them and could work as a REPRoducible EXample (reprex) for your question.

holidays <- c("01-01", "31-12")
df <- data.frame(stringsAsFactors = FALSE,
                 date = c("01-01-2019", "07-31-2019", "12-31-2019",
                          "01-01-2018", "07-31-2018", "12-31-2018"))
library(dplyr)
library(lubridate)
library(stringr)

df %>% 
    mutate(date = mdy(date),
           dummy = if_else(paste(str_pad(day(date), 2, pad = "0"),
                                 str_pad(month(date), 2, pad = "0"),
                                 sep = "-") %in% holidays, 1, 0))
#>         date dummy
#> 1 2019-01-01     1
#> 2 2019-07-31     0
#> 3 2019-12-31     1
#> 4 2018-01-01     1
#> 5 2018-07-31     0
#> 6 2018-12-31     1

Created on 2019-06-17 by the reprex package (v0.3.0)

Hi,

Thank you for your reply. To clarify, I am making one dummy name "Ht" that == 1 on these Canadian holidays from the year 1979 to 2018:

|Holiday| Date

|New Year's Day| Tuesday, January 1, YEAR
|Good Friday| Friday, immediately preceding Easter, YEAR
|Easter Monday| Monday, movable, YEAR
|Victoria Day| Monday, last Monday preceding May 25, YEAR
|Canada Day| Monday, July 1, YEAR
|Labour Day| Monday, First Monday of September, YEAR
|Thanksgiving Day| Monday Second Monday of October, YEAR
|Remembrance Day| Monday, November 11, YEAR
|Christmas Day| Wednesday, December 25, YEAR
|Boxing Day| Thursday, December 26, YEAR

While YEAR = all years from 1979 to 2018.

So I do have problems for both setting the dummy for specific repetitive dates each year + movable dates every year.

timedate::RMetrics will get you the actual dates for most of them and if mutate will create at variable Ht with an ifelse

Thank you so much!!! I will try this from here!!

Hey Andres,

Thank you so much for your reply. I will try these out and if there's any more question I will let you know!

Hi Andres,

Thank you again for the detailed example that helps me a lot with starting up. I tried doing the following and received some error. May you please take a look at my progress whenever time is made available for you?

(1) ###### I created holidays to include all these. While Remembrance day isn't in the system, I put "11-11" following the example you provided. #######

holidays <- c( Easter(1979:2018)

  •          , CACanadaDay(1979:2018)
    
  •          , CAThanksgivingDay(1979:2018)
    
  •          , CACivicProvincialHoliday(1979:2018)
    
  •          , CALabourDay(1979:2018)
    
  •          , CAVictoriaDay(1979:2018)
    
  •          , ChristmasDay(1979:2018)
    
  •          , NewYearsDay(1979:2018)
    
  •          , GoodFriday(1979:2018)
    
  •          , BoxingDay(1979:2018)
    
  •          , "11-11" )
    

****** Warning message: In FUN(X[[i]], ...) : NAs introduced by coercion ******* <-------- I'm not sure what this means, but it seems to be a problem from my "11-11"

(2) ####### Then I created the data frame and put in FIFA$Date as my "date vector" since the name of my dataset is "FIFA", and I have a vector of daily dates called "Date" that is in the format Y%/M%/D%. ------> after that, I got an error message as shown below ######

Date.frame <- data.frame(stringsAsFactors = FALSE, date = FIFA$Date)
library(dplyr)
library(lubridate)
library(stringr)

Date.frame %>%

  • mutate(date = ymd(FIFA$Date),
  •      Ht = if_else(paste(str_pad(day(FIFA$Date), 2, pad = "0"),
    
  •                            str_pad(month(FIFA$Date), 2, pad = "0"),
    
  •                            sep = "-") %in% holidays, 1, 0))
    

***Error in match(x, table, nomatch = 0L) : 'match' requires vector arguments

(3) ###### Lastly, I want to create dummy " Ht_prev1 " & " Ht_nxt1 " as two dummy variables that ==1 when its the 1 previous business day of a non-weekend holiday that I already generated ; and " Ht_nxt1 " would be the next business day. Here's what I tried and the error messages I am getting. ######

METHOD 1

Ht_prev1 <- ifelse(FIFA$Dates == holidays, holidays - 1, FIFA$Dates)
Error in FIFA$Dates == holidays :
comparison (1) is possible only for atomic and list types

Ht_nxt1 <- ifelse(FIFA$Dates == 'holidays' + 1 , FIFA$Dates)
Error in "holidays" + 1 : non-numeric argument to binary operator


METHOD 2

library(dsa)

Holiday(dates=timeDate::holidays, shift=-1)
Error: 'holidays' is not an exported object from 'namespace:timeDate'

Holiday(dates=timeDate::holidays, shift=+1)
Error: 'holidays' is not an exported object from 'namespace:timeDate'


METHOD 3

library(tis)

nextBusinessDay(FIFA$Date, holidays = holidays, goodFriday = F, board = F, inaug = F)
Error in match(ymd(z), holidays, nomatch = 0) :
'match' requires vector arguments

previousBusinessDay(FIFA$Date, holidays = holidays, goodFriday = F, board = F, inaug = F)
Error in match(ymd(z), holidays, nomatch = 0) :
'match' requires vector arguments

The first problem I can see is that you can´t add a character string (i.e. "11-11") to the POSIXct vector that you are creating with those functions, you would have to add that date in a proper format, see this example.

library(timeDate)
holidays <- c(Easter(1979:2018),CACanadaDay(1979:2018))@Data
head(holidays)
#> [1] "1979-04-15 GMT" "1980-04-06 GMT" "1981-04-19 GMT" "1982-04-11 GMT"
#> [5] "1983-04-03 GMT" "1984-04-22 GMT"

# Adding a date in POSIXct format
holidays <- c(as.POSIXct("2019-11-11"), holidays)
head(holidays)
#> [1] "2019-11-11 UTC" "1979-04-15 UTC" "1980-04-06 UTC" "1981-04-19 UTC"
#> [5] "1982-04-11 UTC" "1983-04-03 UTC"

Also, please ask your questions with a Reproducible Example, the way you are sharing your code is very hard to read and understand. If you've never heard of a reprex before, you might want to start by reading this FAQ:

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.