[英]Arrange data frame by all columns using dplyr
I am generating data frames of 1s and 0s as follows:我正在生成 1 和 0 的数据帧,如下所示:
library(tidyverse)
library(glue)
num_var <- 3
rep(list(c(0L, 1L)), num_var) %>%
set_names(glue("var_{seq_len(num_var)}")) %>%
expand.grid() %>%
mutate(total = rowSums(.)) %>%
select(total, everything()) %>%
arrange(total, desc(var_1, var_2, var_3))
#> total var_1 var_2 var_3
#> 1 0 0 0 0
#> 2 1 1 0 0
#> 3 1 0 1 0
#> 4 1 0 0 1
#> 5 2 1 1 0
#> 6 2 1 0 1
#> 7 2 0 1 1
#> 8 3 1 1 1
Created on 2018-01-08 by the reprex package (v0.1.1.9000).由reprex 包(v0.1.1.9000) 于2018年 1 月 1 日创建。
I would need to arrange by the total sum of the variable in ascending order, and then each variable in descending order.我需要按升序排列变量的总和,然后按降序排列每个变量。 This is fairly straightforward using dplyr::arrange()
.使用dplyr::arrange()
非常简单。 However, I would like to have a more robust method of arranging.但是,我想有一个更强大的安排方法。 For example, if num_var
is changed to, then, the final line must also be changed to arrange(total, desc(var_1, var_2, var_3, var_4))
.例如,如果将num_var
更改为,那么最后一行也必须更改为arrange(total, desc(var_1, var_2, var_3, var_4))
。 I have tried using the tidy selector everything()
to arrange as I do with the select()
function, but this errors:我已经尝试使用 tidy 选择器everything()
来安排,就像我使用select()
函数一样,但是这个错误:
library(tidyverse)
library(glue)
num_var <- 3
rep(list(c(0L, 1L)), num_var) %>%
set_names(glue("var_{seq_len(num_var)}")) %>%
expand.grid() %>%
mutate(total = rowSums(.)) %>%
select(total, everything()) %>%
arrange(total, desc(everything()))
#> Error in arrange_impl(.data, dots): Evaluation error: No tidyselect variables were registered.
Created on 2018-01-08 by the reprex package (v0.1.1.9000).由reprex 包(v0.1.1.9000) 于2018年 1 月 1 日创建。
Is there a way to select variables for arranging without naming them all directly?有没有办法选择变量进行排列而不直接命名它们?
arrange
doesn't seem to work with select helper functions directly. arrange
似乎不能直接与选择帮助函数一起使用。 You may use arrange_at
, total
in ascending order, and other variables except total
(select using -one_of("total")
) in descending order:您可以按升序使用arrange_at
、 total
和按降序排列的total
以外的其他变量(使用-one_of("total")
):
arrange_at(vars(total, desc(-one_of("total"))))
# total var_1 var_2 var_3
#1 0 0 0 0
#2 1 1 0 0
#3 1 0 1 0
#4 1 0 0 1
#5 2 1 1 0
#6 2 1 0 1
#7 2 0 1 1
#8 3 1 1 1
could arrange by every column going left to right using this code可以使用此代码按从左到右的每一列进行排列
library(magrittr) ; library(rlang) ; library(dplyr)
data %>% arrange(!!!syms(colnames(.)))
this works since arrange doesnt accept tidyselect syntax and thus must be passed symbols (or maybe also strings) for each of the names这是有效的,因为安排不接受 tidyselect 语法,因此必须为每个名称传递符号(或者也可能是字符串)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.