dplyr 0.7.5 select()行为中的更改
使用命名向量指定列时,dplyr 0.7.5中的select返回与dplyr 0.7.4不同的结果dplyr 0.7.5 select()行为中的更改,r,dplyr,R,Dplyr,使用命名向量指定列时,dplyr 0.7.5中的select返回与dplyr 0.7.4不同的结果 library(dplyr) df <- data.frame(a = 1:5, b = 6:10, c = 11:15) print(df) #> a b c #> 1 1 6 11 #> 2 2 7 12 #> 3 3
library(dplyr)
df <- data.frame(a = 1:5, b = 6:10, c = 11:15)
print(df)
#> a b c
#> 1 1 6 11
#> 2 2 7 12
#> 3 3 8 13
#> 4 4 9 14
#> 5 5 10 15
# a named vector
cols <- c(x = 'a', y = 'b', z = 'c')
print(cols)
#> x y z
#> "a" "b" "c"
# with dplyr 0.7.4
# returns column names with vector values
select(df, cols)
#> a b c
#> 1 1 6 11
#> 2 2 7 12
#> 3 3 8 13
#> 4 4 9 14
#> 5 5 10 15
# with dplyr 0.7.5
# returns column names with vector names
select(df, cols)
#> x y z
#> 1 1 6 11
#> 2 2 7 12
#> 3 3 8 13
#> 4 4 9 14
#> 5 5 10 15
这是一个bug还是一个特性?在我看来,它在0.7.4中可能被认为是一个bug,现在已经修复/更方便用户使用
随着tidyselect的出现,逻辑变得更加复杂。
如果将dplyr::select_vars与新的tidyselect::vars_select进行比较,这些是0.7.4和0.7.5中dplyr:::select.data.frame分别使用的变体,您会发现下面的行丢失了0.7.4中命名和引用字符串的名称:
ind_list <- map_if(ind_list, is_character, match_var, table = vars)
# example:
dplyr:::select.data.frame(mtcars, c(a = "mpg", b = "disp"))
有一行代码处理c的使用:
现在使用tidyselect,我们有一些额外的处理,请参阅
特别是,vars_select_eval有下面一行,其中处理c的使用:
与上面使用rlang::eval_tidy时不同,is_字符在后续代码中不再有效
如果您在rlang中查看这些函数,那么鉴于上述情况,overscope_eval_next被软性弃用以支持eval_tidy可能会让您感到困惑。但在这里,我想tidyselect还没有被清理,但命名不一致等问题也必须得到解决,所以这不仅仅是一行调用的重写。但最终,eval_tidy现在可以用同样的方式使用,而且很可能会用同样的方式使用。我在发行说明中看到:dplyr现在依赖于新的tidyselect软件包来启用select、rename、pull及其变体2896。可能是意外的副作用。这可能是显而易见的,也不能回答问题,但仅供参考,您可以使用selectdf、unnamecolsrenamedf、cols返回到旧行为,但不起作用
dplyr:::select.data.frame(mtcars, c(a = mpg, b = disp))
# (here the names are indeed "a" and "b" afterwards)
ind_list <- map_if(ind_list, !is_helper, eval_tidy, data = names_list)
[[1]]
a b
"mpg" "disp"
ind_list <- map_if(quos, !is_helper, overscope_eval_next, overscope = overscope)
[[1]]
a b # these are the names
1 3 # these are the positions of the selected cols