2

我有几个相似的数据框,它们有很多共同的列。我想从任何表中选择并重命名这些列的子集。

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 框架?

4

2 回答 2

1

我想你想要:

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
于 2017-07-13T14:51:03.317 回答
0

Lionel 对这个问题group_by 的回答 by a vector of characters using tidy evaluation semantics 提供了答案

mtcars %>% 
    select(!!! rlang::syms(my_select_rename))
于 2017-07-17T11:12:45.403 回答