Problem:
I have a multi-year dataset where I want to remove a range of dates (e.g. May 1 - August 31) across all years. I want to keep dates from April 30 to September 1 across all years.
Example of data:
dat <- data.frame(
"Date" = c("2000-08-01","2000-11-02","2001-08-03","2001-11-04","2002-06-05","2002-02-06","2000-08-07","2000-02-08","2001-06-09","2001-11-10"),
"Type" = c("A", "B", "C", "B", "C", "B", "A", "A", "C", "B"),
"Num" = c(8,6,5,6.5,5,5.5,6,7,4,3)
)
dat
What I've tried:
I created an object with the dates for May 1 across all years, and using a for loop to remove the number of days following that until August 31. I know this is an extremely roundabout way, so I'm wondering if there's an easier solution as this is a rather large dataset.
dates_start <- as.Date(c("2000-05-01", "2001-05-01", "2002-05-01"))
for(i in 1:length(dates_start)){
dat <- dat[!(as.Date(dat$Date) >= (dates_start[i] + 123)),]
}
Edit:
I plan to run this filter/removal multiple times to obtain different datasets, and in some cases, I want to remove dates in the middle of the month (e.g. April 15 to August 16), so just removing certain months would not work here.