'Use a variable name as function argument
I would like to write a unique function to (among other things) filter a database for different variables at different threshold. I found a way to indicate the variable I want to filter on in the function itself, but not sure is the best way.
How should I go about it?
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)
}
Expected result:
advanced_filter(example_db,value_1,2)
name value_1 value_2
1 C 3 1
My attempt:
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)
Solution 1:[1]
Perhaps you are making this more complicated than it needs to be:
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
Created on 2022-01-28 by the reprex package (v2.0.1)
Solution 2:[2]
If you want to be able to use the vector name as a string, either fully qualified - or just as a symbol you can use the following Base R solution:
# 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)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Allan Cameron |
| Solution 2 | hello_friend |
