I am trying to use case_when() inside a function. The idea for the function is to take a specified column, search it for a particular term, and then return true if the term is in that column, and false if it is not.
However, I am struggling with trying to make case_when refer to a column that is passed in at the level of the function - at least, the way I have succeeded doing it seems a bit hacky, in that I pass in a character string and then convert it to a symbol, and then pass it to case_when. Is there a way of referring to a column more dynamically directly in case_when?:
test_tibble <- tibble(col_a = c("Here I am", "Nope", "Nothing", "I am here"))
test_function <- function(target_item,
data = test_tibble,
variable_name,
prefix = "check_") {
variable_name <- as.symbol(variable_name)
data <-
data %>%
mutate("{prefix}{target_item}" := case_when(grepl(target_item, {{variable_name}}) ~ TRUE,
TRUE ~ FALSE))
return(data)
}
test_function("ere", variable_name = "col_a")
In the real use case, I might need to refer to many different named columns, so it is important that I can specify the column name at the top of the function.
CodePudding user response:
There's nothing specific about case_when
when it comes to passing in column names to a function. It all depends on how you want to pass in the column name. In fact it's not really clear why you are using case_when
at all. You can just use grepl
directory since it will return a TRUE/FALSE value already.
If you want to pass in a string, use
test_function <- function(target_item,
data = test_tibble,
variable_name,
prefix = "check_") {
data <-
data %>%
mutate("{prefix}{target_item}" := case_when(grepl(target_item, .data[[variable_name]]) ~ TRUE,
TRUE ~ FALSE))
return(data)
}
test_function("ere", variable_name = "col_a")
If you want to pass in a symbol, use
test_function <- function(target_item,
data = test_tibble,
variable_name,
prefix = "check_") {
data <-
data %>%
mutate("{prefix}{target_item}" := case_when(grepl(target_item, {{variable_name}}) ~ TRUE,
TRUE ~ FALSE))
return(data)
}
test_function("ere", variable_name = col_a)
CodePudding user response:
you may also use get
function and remove the {{ }}
and as.symbol
test_tibble <- tibble(col_a = c("Here I am", "Nope", "Nothing", "I am here"))
#> Error in tibble(col_a = c("Here I am", "Nope", "Nothing", "I am here")): could not find function "tibble"
test_function <- function(target_item,
data = test_tibble,
variable_name,
prefix = "check_") {
data <-
data %>%
mutate("{prefix}{target_item}" := case_when(grepl(target_item, get(variable_name)) ~ TRUE,
TRUE ~ FALSE))
return(data)
}
test_function("ere", variable_name = "col_a")
Created on 2023-02-02 with reprex v2.0.2