library(tidyr) d = seq(as.Date("2018-01-01"),Sys.Date()+365,by='day') saturdayList = d[weekdays(d)=='Saturday'] altSaturdayList = data.frame(lapply(split(saturdayList, format(saturdayList, "%Y-%m")), function(x) na.omit(x[c(2,4)]))) altSaturdayList = subset(gather(altSaturdayList),select = c(value)) HolidayList = rbind(HolidayList, setNames(altSaturdayList, "Holidays")) HolidayList = rbind(HolidayList, setNames(as.data.frame(as.Date("2018-11-06")), "Holidays")) HolidayList = arrange(unique(HolidayList),Holidays) rm(d) rm(saturdayList) rm(altSaturdayList)
The above code generates a list of dates which contain 2nd and 4th saturday of every month within that date range.
As you can tell I have generated and deleted intermediate variables like
altSaturdayList. I was wondering how I can make this code better and efficient?
I learned about
%>% operator but I don't know how I can do it with no input.
Any tips to improve the code is welcomed. Thanks