Q:

How to create a dummy variable for date interval

I have been trying to generate a dummy variable from the data column for interval. 
Sample data 
Date <- seq(as.Date("1988-01-01"), as.Date("2018-12-31"), by="1 day") 
DATASET <- data.frame(rnorm(11323), Date) 

I would like to create an interval: 20-04 : 20-08 for each year codes as 1. I would be grateful for the hint with code for doing this.

A:

You could compare the day of the year. In base R that would be
DATASET$day_of_year <- as.integer(format(DATASET$Date, "%j"))
DATASET$flag <- +(with(DATASET, ifelse(as.integer(format(Date, "%Y")) %% 4 == 0 ,
                           day_of_year %in% 111:233, day_of_year %in% 110:232)))

For leap years 20-04 is 111th day of the year and 20-08 is 233rd day and for rest of the years they are 110 and 232 respectively. We assign 1 when the date is between those 2 values. 

