如何传递命名向量到dplyr :: select使用quosures? [英] How to pass a named vector to dplyr::select using quosures?

查看:294
本文介绍了如何传递命名向量到dplyr :: select使用quosures?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用旧的 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))

#这是所需的输出
#A tibble:4 x 2
newbar newfoo
< int> < INT>
1 10 0
2 11 1
3 12 2
4 13 3

我尝试使用quosures和splicing做类似的事情 - 选择列的效果很好,但是向量的名称(因此同时重命名列)似乎被忽略。以下两个返回数据框,列名为 bar foo ,但不是 newbar newfoo

  move_stuff2<函数(df,newnames){
select(df,!!! newnames)
}

#返回df与列栏和foo
move_stuff2(my_data,quo (my_newnames))
move_stuff2(my_data,quos(my_newnames))

有没有办法使用新的NSE方法来命名和重新排序列的命名向量?

解决方案

quo (或 quos for multiple)用于未引用的变量名称,而不是字符串。要将字符串转换为quosure,请使用 sym (或 syms ),并使用 !! !!! 适用于unquote或unquote-splice:

  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& (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

对于未引用的变量名,



< pre class =lang-r prettyprint-override> 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


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?

解决方案

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

这篇关于如何传递命名向量到dplyr :: select使用quosures?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆