I have a data frame in my R environment that I would like to subset based on a specific criteria -a sort of conditional filter. My data frame is a panel dataset of daily values for each day between 2004-2014. Each day in the data frame is a separate observation. Each year has 366 days. I would like to subset the data such that only the leap years retain the 366th day in the panel data. There are three leap years in that time range -2004, 2008, 2012. I have a separate column for the year and the day of the year. In other words, I need a script that will return a dataset without the 366th day but only for each year other than 2004, 2008, and 2012.
I've managed to accomplish this the following way: I pasted my day and year columns together (e.g. "2006-366") and simply used dplyr's filter command to subset each year (2005-366, 2006-366, 2007-366, 2009-366, 2010-366, 2011-366, 2013-366, 2014-366). This however is an awfully crude method. I was hoping someone could point me in the right direction here. Here's some reproducible data along with the workflow I used.
#Create DF
year<-rep(c(2004:2014), each=366)
day<-rep(c(1:366))
df<-data.frame(day, year)
#My crude method
df $reduc<-paste(df$year, df$day, sep="-")
df <-df %>%
filter(reduc!="2005-366") %>%
filter(reduc!="2006-366") %>%
filter(reduc!="2007-366") %>%
filter(reduc!="2009-366") %>%
filter(reduc!="2010-366") %>%
filter(reduc!="2011-366") %>%
filter(reduc!="2013-366") %>%
filter(reduc!="2014-366")