Problem when function argument is the same as dataframe column name in dplyr

21 views
Skip to first unread message

Weicheng Zhu

unread,
Apr 25, 2017, 11:52:54 AM4/25/17
to manipulatr
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)
#   year doy
# 1 2010   3
# 2 2010 200
# 3 2010 365

f2 = function(df, yr, doy1){
  df %>% filter(year == yr, doy == doy1) 
}
f2(df, 2010, 3)
# year doy
# 2010   3

Best,
Weicheng

Maximilian

unread,
Apr 26, 2017, 1:18:57 AM4/26/17
to manipulatr
Hi,

you simply have to use local() for the variable you pass into the function:


f1 = function(df, yr, doy){
  browser()
  df %>% filter(year == yr, doy == local(doy))
}


Best
Max

Weicheng Zhu

unread,
Apr 26, 2017, 10:21:03 AM4/26/17
to manipulatr
Thanks Max! This is the solution I want.
Reply all
Reply to author
Forward
0 new messages