有很多选项供您选择。
library(dplyr)
VARS1 <- quote(c(Sepal.Length, starts_with("Petal")))
VARS2 <- expr(c(Sepal.Length, starts_with("Petal")))
VARS3 <- quo(c(Sepal.Length, starts_with("Petal")))
输出
> iris %>% mutate(across(!!VARS1, as.character)) %>% str()
'data.frame': 150 obs. of 5 variables:
$ Sepal.Length: chr "5.1" "4.9" "4.7" "4.6" ...
$ Sepal.Width : num 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
$ Petal.Length: chr "1.4" "1.4" "1.3" "1.5" ...
$ Petal.Width : chr "0.2" "0.2" "0.2" "0.2" ...
$ Species : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
> iris %>% mutate(across(!!VARS2, as.character)) %>% str()
'data.frame': 150 obs. of 5 variables:
$ Sepal.Length: chr "5.1" "4.9" "4.7" "4.6" ...
$ Sepal.Width : num 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
$ Petal.Length: chr "1.4" "1.4" "1.3" "1.5" ...
$ Petal.Width : chr "0.2" "0.2" "0.2" "0.2" ...
$ Species : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
> iris %>% mutate(across(!!VARS3, as.character)) %>% str()
'data.frame': 150 obs. of 5 variables:
$ Sepal.Length: chr "5.1" "4.9" "4.7" "4.6" ...
$ Sepal.Width : num 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
$ Petal.Length: chr "1.4" "1.4" "1.3" "1.5" ...
$ Petal.Width : chr "0.2" "0.2" "0.2" "0.2" ...
$ Species : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...