I would like to use dplyr::arrange
to sort the rows of a data frame based on values in specific columns. I want to choose the columns based on position rather than column name, as the column names will vary based on the input. I have tried to adapt the suggestions for dplyr::select
found here (dplyr: select columns by position in NSE), but my code just returns the original data frame with no changes. Here is my data frame and the code I've used to sort it:
df <- structure(list(D7_ctrl_v_D6_ctrl_deg = structure(c(1L, 2L, 3L,
1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L,
2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L), .Label = c("down", "unchanged",
"up"), class = "factor"), D7_OE_v_D7_ctrl_deg = structure(c(1L,
1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L,
3L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L), .Label = c("down", "unchanged",
"up"), class = "factor"), D7_OE_v_D6_ctrl_deg = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("down", "unchanged",
"up"), class = "factor"), Freq = c(584L, 1841L, 375L, 636L, 331L,
0L, 44L, 0L, 0L, 0L, 420L, 600L, 208L, 9164L, 280L, 391L, 410L,
0L, 0L, 0L, 69L, 0L, 448L, 746L, 297L, 2362L, 715L)), class = "data.frame", row.names = c(NA,
-27L))
## these all fail
df %>% dplyr::arrange(1)
df %>% dplyr::arrange(c(1))
df %>% dplyr::arrange(!!"1")
df %>% dplyr::arrange(!!c(1))
I'm guessing the difference has something to do with the difference between data-masking
used by arrange
and tidy-select
used by select
, but I can't figure out if there is a way to pick columns by position in arrange
. Any suggestions would be appreciated. Thanks.