I am trying to create a function that will accept a data.frame and an integer representing the value for a specific year (such as 2015). The function will then use Dplyr and return a filtered data set.
The following is some test data, libraries and a non-parameterised dplyr call:
library(dplyr)
library(lazyeval)
library(lubridate
vct_dates <- c("2015-04-30", "2015-04-30", "2012-04-30", "2010-04-30")
vct_numbers <- c(21, 45, 103, 214)
df_test <- data.frame(date = vct_dates, value = vct_numbers)
df_test %>% filter(year(date) == 2015)
Now, the following is my attempt to encapsulate this functionality into a function:
fn_filter_year <- function(df_data, intYear) {
filter_condition <- interp(quote(year(x) == y), x=as.name("date"), y = intYear)
df_data %>% filter_(filter_condition)
return(df_data)
}
I am getting an error that "could not find the function 'year' " ??
Any suggestions would be really appreciated.