select 并重命名存储在变量中

select and rename stored in variable

我有几个相似的数据框,其中有许多共同的列。我想 select 并从任何 table.

中重命名这些列的子集
library(tidyverse)
mtcars %>% 
select(my_mpg = mpg, 
       cylinders = cyl,
       gear)

是否可以做类似

的事情
my_select_rename <- c("my_mpg"="mpg","cylinders"="cyl","gear")

mtcars %>% 
   select_(.dots = my_select_rename)

而是改用 tidyeval 框架?

我想你想要:

my_select <- c("mpg","cyl","gear")
my_select_rename <- c("my_mpg","cylinders","gear")

mtcars %>% 
   select_at(vars(my_select)) %>%
   setNames(., my_select_rename)

                    my_mpg cylinders gear
Mazda RX4             21.0         6    4
Mazda RX4 Wag         21.0         6    4
Datsun 710            22.8         4    4
Hornet 4 Drive        21.4         6    3
Hornet Sportabout     18.7         8    3

lionel 对这个问题的回答 提供了答案

mtcars %>% 
    select(!!! rlang::syms(my_select_rename))