Suppose I have the dataframe:
data = data.frame(A = c(3,2,1), B = c(2,1,1), C = c(1,2,1)).
I would like to arrange the data according to a variable number of columns. Perhaps:
arranging_columns <- c('B')
…OR…
arranging_columns <- c('C', 'A') *(ie, first arrange by C, then by A)*
I do not know how many columns I want to arrange by. Perhaps 1 column, perhaps 2 columns.
How can I achieve this using the dplyr::arange(…) function?
I have tried to pass a vector of arranging_columns to the function dplyr::arrange(…) and then to somehow unwrap this vector into multiple arguments, like:
arranging_columns <- c('C', 'B')
arranged <- dplyr::arrange(data, {arranging_columns})
Desired output:
A B C
1 1 1
3 2 1
2 1 2
But I just get an error saying:
Error in `dplyr::arrange()`:
ℹ In argument: `..1 = arranging_columns`.
Caused by error:
! `..1` must be size 3 or 1, not 2.
Isaac Feldman is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.