使用变量名作为函数参数
Use a variable name as function argument
我想编写一个独特的函数来(除其他外)在不同的阈值下为不同的变量过滤数据库。我找到了一种方法来指示我想在函数本身中过滤的变量,但不确定是不是最好的方法。
我该怎么办?
example_db <- data.frame(name=c("A","B","C"),
value_1=c(1,2,3),
value_2=c(2,3,1))
advanced_filter <- function(data,variable,limit){
require(dplyr)
data <- data %>%
dplyr::filter(variabe>limit)
return(data)
}
预期结果:
advanced_filter(example_db,value_1,2)
name value_1 value_2
1 C 3 1
我的尝试:
advance_filter <- function(data,variable,limit){
require(dplyr)
f <- paste(variable, ">", limit)
data <- data %>%
dplyr::filter_(f)
return(data)
}
advance_filter(example_db,"value_1",2)
也许你把它弄得比需要的更复杂了:
advanced_filter <- function(data, variable, limit) {
data[data[variable] > limit,]
}
advanced_filter(example_db, "value_1", 2)
#> name value_1 value_2
#> 3 C 3 1
由 reprex package (v2.0.1)
于 2022-01-28 创建
Allan Cameron 的答案显然是正确的,只需要基础 R,为了后代,这里是整洁的版本。
example_db <- data.frame(name=c("A","B","C"),
value_1=c(1,2,3),
value_2=c(2,3,1))
advanced_filter <- function(data,variable,limit){
require(dplyr)
vbl <- enquo(variable)
data %>%
dplyr::filter(!!vbl > limit)
}
advanced_filter(example_db,value_1,2)
#> Loading required package: dplyr
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
#> name value_1 value_2
#> 1 C 3 1
由 reprex package (v2.0.1)
于 2022-01-28 创建
或者,按照下面@TimTeaFan 的评论:
example_db <- data.frame(name=c("A","B","C"),
value_1=c(1,2,3),
value_2=c(2,3,1))
advanced_filter <- function(data,variable,limit){
require(dplyr)
data %>%
dplyr::filter({{variable}} > limit)
}
advanced_filter(example_db,value_1,2)
#> Loading required package: dplyr
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
#> name value_1 value_2
#> 1 C 3 1
由 reprex package (v2.0.1)
于 2022-01-28 创建
如果您希望能够将矢量名称用作字符串,无论是完全限定的 - 还是仅作为符号,您可以使用以下 Base R 解决方案:
# Function:
advanced_filter <- function(data, variable, limit) {
if(is.character(substitute(variable))){
variable_str <- variable
}else{
variable_str <- gsub(".*\$", "", deparse(substitute(variable)))
}
res <- data[data[,variable_str] > limit,]
return(res)
}
# Application:
advanced_filter(example_db, "value_1", 2)
advanced_filter(example_db, value_1, 2)
advanced_filter(example_db, example_db$value_1, 2)
我想编写一个独特的函数来(除其他外)在不同的阈值下为不同的变量过滤数据库。我找到了一种方法来指示我想在函数本身中过滤的变量,但不确定是不是最好的方法。
我该怎么办?
example_db <- data.frame(name=c("A","B","C"),
value_1=c(1,2,3),
value_2=c(2,3,1))
advanced_filter <- function(data,variable,limit){
require(dplyr)
data <- data %>%
dplyr::filter(variabe>limit)
return(data)
}
预期结果:
advanced_filter(example_db,value_1,2)
name value_1 value_2
1 C 3 1
我的尝试:
advance_filter <- function(data,variable,limit){
require(dplyr)
f <- paste(variable, ">", limit)
data <- data %>%
dplyr::filter_(f)
return(data)
}
advance_filter(example_db,"value_1",2)
也许你把它弄得比需要的更复杂了:
advanced_filter <- function(data, variable, limit) {
data[data[variable] > limit,]
}
advanced_filter(example_db, "value_1", 2)
#> name value_1 value_2
#> 3 C 3 1
由 reprex package (v2.0.1)
于 2022-01-28 创建Allan Cameron 的答案显然是正确的,只需要基础 R,为了后代,这里是整洁的版本。
example_db <- data.frame(name=c("A","B","C"),
value_1=c(1,2,3),
value_2=c(2,3,1))
advanced_filter <- function(data,variable,limit){
require(dplyr)
vbl <- enquo(variable)
data %>%
dplyr::filter(!!vbl > limit)
}
advanced_filter(example_db,value_1,2)
#> Loading required package: dplyr
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
#> name value_1 value_2
#> 1 C 3 1
由 reprex package (v2.0.1)
于 2022-01-28 创建或者,按照下面@TimTeaFan 的评论:
example_db <- data.frame(name=c("A","B","C"),
value_1=c(1,2,3),
value_2=c(2,3,1))
advanced_filter <- function(data,variable,limit){
require(dplyr)
data %>%
dplyr::filter({{variable}} > limit)
}
advanced_filter(example_db,value_1,2)
#> Loading required package: dplyr
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
#> name value_1 value_2
#> 1 C 3 1
由 reprex package (v2.0.1)
于 2022-01-28 创建如果您希望能够将矢量名称用作字符串,无论是完全限定的 - 还是仅作为符号,您可以使用以下 Base R 解决方案:
# Function:
advanced_filter <- function(data, variable, limit) {
if(is.character(substitute(variable))){
variable_str <- variable
}else{
variable_str <- gsub(".*\$", "", deparse(substitute(variable)))
}
res <- data[data[,variable_str] > limit,]
return(res)
}
# Application:
advanced_filter(example_db, "value_1", 2)
advanced_filter(example_db, value_1, 2)
advanced_filter(example_db, example_db$value_1, 2)