-1

I would like to create a seasonal dummy. Specifically, I want the variable to equal 1 every June and 0 otherwise. I would like the dummy variable output stored in mat1[,2]

Setup:

start = as.Date("1926-07-01")
end = as.Date("2019-07-01")
dates = seq(from = start, to = end, by = "month")
mat1 <- matrix(nrow =1117, ncol =2)
mat1[,1] = dates

JC3019
  • 363
  • 1
  • 9

1 Answers1

1

Extract the month from dates and check if the month is "June".

data.frame(dates = dates, isJune = +(format(dates, "%m") == "06"))

We can also use regex to identify if it's the 6th month

data.frame(dates = dates, isJune = +(grepl(".*-06-.*", dates)))
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213