I am generating data frames of 1s and 0s as follows:
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).
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()
. 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))
. I have tried using the tidy selector everything()
to arrange as I do with the select()
function, but this errors:
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).
Is there a way to select variables for arranging without naming them all directly?