let's say i have a lot of columns, but i usually do some calculations on a specific subset of columms
my_df <- mtcars %>%
filter(if_all(contains("p"), is.numeric)) %>%
mutate(across(contains("p"), as.character))
i want to assign that tidy-select to a variable so i can use it later on, something like:
# doesn't work
my_cols <- contains("p")
my_df <- mtcars %>%
filter(if_all(my_cols, is.numeric)) %>%
mutate(across(my_cols, as.character))
many thanks
CodePudding user response:
You can use vars_select
for a tidyselect
option:
my_cols <- tidyselect::vars_select(names(mtcars), contains("p"))
Otherwise, use grepl
or str_detect
:
names(mtcars)[str_detect(names(mtcars), "p")]
names(mtcars)[grepl("p", names(mtcars))]
CodePudding user response:
You could quote the tidy-select syntax and inject them with !!
in the functions that support tidy selections.
my_cols <- quote(contains("Sepal"))
iris %>%
filter(if_all(!!my_cols, `>`, 4))
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1 5.7 4.4 1.5 0.4 setosa
2 5.2 4.1 1.5 0.1 setosa
3 5.5 4.2 1.4 0.2 setosa