Transform "other_Species"
in a symbol first. And all_of
accepts only one argument at a time.
Another option, like it is said in comment, is to combine both character vectors with c()
.
你说all_of()
一次只接受一个字符串是什么意思?它接受一个字符向量,这样你就可以在函数中使用select(all_of(c(col, paste0("other_", col))))
,如果你正在传递一个字符串,就不需要转换成符号.
suppressPackageStartupMessages(
library(tidyverse)
)
iris_df <- iris %>%
mutate(other_Species = Species)
iris_fn <- function (df, col){
other_col <- sym(paste0("other_", col))
df%>%
select(all_of(col), all_of(other_col))
}
iris_fn(df = iris_df, col = "Species") %>% head()
#> Species other_Species
#> 1 setosa setosa
#> 2 setosa setosa
#> 3 setosa setosa
#> 4 setosa setosa
#> 5 setosa setosa
#> 6 setosa setosa
创建于2024-01-25年第reprex v2.0.2页