我有这样的dplyr代码
data <- load_data(country = "us") %>%
filter(number > 1)
Run Code Online (Sandbox Code Playgroud)
我想像这样从此调用创建函数
test <- function(country_code = "us") {
data <- load_data(country = country_code) %>%
filter(number > 1)
}
Run Code Online (Sandbox Code Playgroud)
但我想为该数字过滤器添加偶数参数。通常,我的操作方式与country_code相同。但我希望能够呼叫偶数= 0或数字小于1,依此类推。
因此,问题是如何处理函数调用中的刨丝器(少/等)符号?
应该是类似test <- function(country_code = "us", number > 0)或
test <- function(country_code = "us", number <= -10)
Run Code Online (Sandbox Code Playgroud)
您可以使用...符号将其他参数传递给过滤器功能。
例:
test <- function(path, country_code = "us", ...) {
read_csv(path) %>%
filter(...)
}
test('somepath', country_code = "us", number <= -10)
Run Code Online (Sandbox Code Playgroud)