如何使用quosures将命名向量传递给dplyr::select函数?

10

使用旧的select_()函数,我可以将一个命名向量传递到select中,同时更改位置和列名称:

my_data  <- data_frame(foo = 0:10, bar = 10:20, meh = 20:30)
my_newnames  <-  c("newbar" = "bar", "newfoo" = "foo")

move_stuff  <- function(df, newnames) {
    select_(df, .dots = newnames)
}

move_stuff(my_data,  newnames = my_newnames) )

# this is the desired output
# A tibble: 4 x 2
  newbar  newfoo
   <int>   <int>
1     10       0
2     11       1
3     12       2
4     13       3

我尝试使用quosures和splicing做类似的事情--选择列非常有效,但向量的名称(从而同时重命名列)似乎被忽略了。以下两个示例返回具有名为barfoo的列的数据帧,但没有newbarnewfoo

move_stuff2  <- function(df, newnames) {
  select(df, !!!newnames)
}

# returns df with columns bar and foo
move_stuff2(my_data, quo(my_newnames))
move_stuff2(my_data, quos(my_newnames))
有没有一种方法可以使用新的 NSE 方法来重命名和重新排序列,以便使用命名向量?
1个回答

12

quo(或用quos表示复数)适用于未引用的变量名,而不是字符串。要将字符串转换为quo表达式,请使用sym(或syms),并根据需要使用!!!!! 进行非引用或非引用展开:

library(dplyr)

my_data  <- data_frame(foo = 0:10, bar = 10:20, meh = 20:30)
my_newnames  <-  c("newbar" = "bar", "newfoo" = "foo")

对于字符串,

move_stuff_se <- function(df, ...){
     df %>% select(!!!rlang::syms(...))
}

move_stuff_se(my_data, my_newnames)
#> # A tibble: 11 x 2
#>    newbar newfoo
#>     <int>  <int>
#>  1     10      0
#>  2     11      1
#>  3     12      2
#>  4     13      3
#>  5     14      4
#>  6     15      5
#>  7     16      6
#>  8     17      7
#>  9     18      8
#> 10     19      9
#> 11     20     10

对于未加引号的变量名,

move_stuff_nse <- function(df, ...){
    df %>% select(!!!quos(...))
}

move_stuff_nse(my_data, newbar = bar, newfoo = foo)
#> # A tibble: 11 x 2
#>    newbar newfoo
#>     <int>  <int>
#>  1     10      0
#>  2     11      1
#>  3     12      2
#>  4     13      3
#>  5     14      4
#>  6     15      5
#>  7     16      6
#>  8     17      7
#>  9     18      8
#> 10     19      9
#> 11     20     10

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接