Hi all,
I have a problem that bothered me for a long time and I would like to
hear advices on how to handle this elegantly.
The problem occurs when the argument of a function passed to the `filter` function
happens to be the same as a column name in the dataframe as shown in the following
example. I initially want to filter based on the user input value of the `doy`, but apparently
`dplyr` uses both `doy` in the `filter` function from the data frame.
My current way is to rename the function parameter as shown in `f2` to avoid using the
same names that occur in the dataframe. But I really don't like the syntax `doy1`.
I'm wondering whether there is a better way to handle this.
Thanks for any suggestions!
df = data.frame(year = rep(rep(c(2010:2011), each = 3)),
doy = c(3,200, 365, 3, 100, 365))
f1 = function(df, yr, doy){
df %>% filter(year == yr, doy == doy) ## Problem
}
f1(df, 2010, 3)
f2 = function(df, yr, doy1){
df %>% filter(year == yr, doy == doy1)
}
f2(df, 2010, 3)
# year doy
# 2010 3