How to pass a named vector to dplyr::select using

2019-01-19 00:22发布

Using the old select_() function, I could pass a named vector into select and change position and column names at once:

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

I tried doing something similar using quosures and splicing--selecting columns works great, but the names of the vectors (and thus renaming columns at the same time) seems to be ignored. Both of the following return data frames with columns named bar and foo, but not newbar and newfoo:

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))

Is there a way to use a named vector using the new NSE methodology to both rename and reorder columns?

标签: r dplyr nse rlang
1条回答
Luminary・发光体
2楼-- · 2019-01-19 01:07

quo (or quos for multiple) is for unquoted variable names, not strings. To convert strings to quosures use sym (or syms), and use !! or !!! as appropriate to unquote or unquote-splice:

library(dplyr)

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

For strings,

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

For unquoted variable names,

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
查看更多
登录 后发表回答