将变量名称向量传递给 dplyr 中的排列() [英] Pass a vector of variable names to arrange() in dplyr
问题描述
我想传递 arrange()
{dplyr} 一个变量名称向量来排序.通常我只是输入我想要的变量,但我正在尝试创建一个函数,其中排序变量可以作为函数参数输入.
I want to pass arrange()
{dplyr} a vector of variable names to sort on. Usually I just type in the variables I want, but I'm trying to make a function where the sorting variables can be input as a function parameter.
df <- structure(list(var1 = c(1L, 2L, 2L, 3L, 1L, 1L, 3L, 2L, 4L, 4L
), var2 = structure(c(10L, 1L, 8L, 3L, 5L, 4L, 7L, 9L, 2L, 6L
), .Label = c("b", "c", "f", "h", "i", "o", "s", "t", "w", "x"
), class = "factor"), var3 = c(7L, 5L, 5L, 8L, 5L, 8L, 6L, 7L,
5L, 8L), var4 = structure(c(8L, 5L, 1L, 4L, 7L, 4L, 3L, 6L, 9L,
2L), .Label = c("b", "c", "d", "e", "f", "h", "i", "w", "y"),
class = "factor")), .Names = c("var1", "var2", "var3", "var4"),
row.names = c(NA, -10L), class = "data.frame")
# this is the normal way to arrange df with dplyr
df %>% arrange(var3, var4)
# but none of these (below) work for passing a vector of variables
vector_of_vars <- c("var3", "var4")
df %>% arrange(vector_of_vars)
df %>% arrange(get(vector_of_vars))
df %>% arrange(eval(parse(text = paste(vector_of_vars, collapse = ", "))))
推荐答案
Hadley 没有在帮助文件中明确说明这一点——仅在他的 NSE 小插图中.下划线后的函数版本使用标准评估,因此您可以将字符串向量等传递给它们.
Hadley hasn't made this obvious in the help file--only in his NSE vignette. The versions of the functions followed by underscores use standard evaluation, so you pass them vectors of strings and the like.
如果我正确理解您的问题,您只需将 arrange()
替换为 arrange_()
即可.
If I understand your problem correctly, you can just replace arrange()
with arrange_()
and it will work.
具体来说,在执行时将字符串向量作为 .dots
参数传递.
Specifically, pass the vector of strings as the .dots
argument when you do it.
> df %>% arrange_(.dots=c("var1","var3"))
var1 var2 var3 var4
1 1 i 5 i
2 1 x 7 w
3 1 h 8 e
4 2 b 5 f
5 2 t 5 b
6 2 w 7 h
7 3 s 6 d
8 3 f 8 e
9 4 c 5 y
10 4 o 8 c
========== 2018 年 3 月更新 ==============
========== Update March 2018 ==============
在 dplyr 中使用标准评估版本,正如我在此处展示的那样现在被认为已弃用.您可以阅读Hadley 的编程小插图以了解新方法.基本上,您将使用 !!
取消引用一个变量或使用 !!!
取消引用 arrange()
内的变量向量.
Using the standard evaluation versions in dplyr as I have shown here is now considered deprecated. You can read Hadley's programming vignette for the new way. Basically you will use !!
to unquote one variable or !!!
to unquote a vector of variables inside of arrange()
.
当您传递这些列时,如果它们是空的,请使用 quo()
引用它们作为一个变量或使用 quos()
引用它们作为向量.不要使用引号.请参阅 Akrun 的回答.
When you pass those columns, if they are bare, quote them using quo()
for one variable or quos()
for a vector. Don't use quotation marks. See the answer by Akrun.
如果您的列已经是字符串,则使用 rlang::sym()
为单个列或rlang::syms()
为向量命名.请参阅克里斯托斯的答案.您还可以将 as.name()
用于单个列.不幸的是,在撰写本文时,有关如何使用 rlang::sym()
的信息尚未包含在我上面链接的小插图中(最终它将在可变参数准引用"部分根据他的草稿).
If your columns are already strings, then make them names using rlang::sym()
for a single column or rlang::syms()
for a vector. See the answer by Christos. You can also use as.name()
for a single column. Unfortunately as of this writing, the information on how to use rlang::sym()
has not yet made it into the vignette I link to above (eventually it will be in the section on "variadic quasiquotation" according to his draft).
这篇关于将变量名称向量传递给 dplyr 中的排列()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!