I want to make a custom function that I can apply to a range of variables in a data frame using the var1:var20
tidyselect syntax, but I'm running into an error. A basic example below:
library(dplyr)
mtcars
test_func <- function(start_var,end_var) {
mutate(across(start_var:end_var, ~ifelse(.x!=0,.x+1,.x)))
}
mtcars %>% test_func(mpg,hp)
The intended result (without using the function) is:
mtcars %>%
mutate(across(mpg:hp, ~ifelse(.x!=0,.x+1,.x)))
Using the function results in the following error message:
error in test_func(., mpg, hp) : unused argument (hp)