I have a data frame in my R environment that I would like to multiply based on certain criteria - a kind of conditional filter. My data frame is a set of data from daily values for each day between 2004-2014. Each day in a data frame is a separate observation. Each year has 366 days. I would like a subset of the data so that only leap years save the 366th day in the panel data. There are three leap years in this time interval -2004, 2008, 2012. I have a separate column for the year and day of the year. In other words, I need a script that will return a dataset without the 366th day, but only for every year except 2004, 2008 and 2012.
I managed to accomplish this as follows: I inserted my day and year columns together (for example, "2006-366") and simply used the dplyr 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 a very crude method. I was hoping someone could point me in the right direction. Here are some reproducible data along with the workflow that I used.
year<-rep(c(2004:2014), each=366)
day<-rep(c(1:366))
df<-data.frame(day, year)
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")
source
share