I would like to write a function which uses dplyr::filter() within the function. When writing the function I ran into an issue with using a parameter name in the function that is also a name of one of the columns of the data frame I am filtering.
Suppose I call the data frame to be filtered dat:
library(dplyr)
dat <- data.frame(
a = c(1:10),
b = c(2,2,2,2,2,3,1,1,4,4)
)
and name the function test.filter(),
test.filter <- function(b, test.data = dat){
dat.t <- filter(test.data,
b == b)
return(dat.t)
}
Here I am passing a value b to the function and asking it to filter the column b based on the value b. I believe the function
test.filter(b = 4,
test.data = dat)
should produce the same result as
filter(dat,
b == 4)
However this is not the case. I am wondering if there is something I am not considering in terms of the scope of a function. Any help is appreciated!