3

I want to sort a data frame by a variable number of columns. For example, with the data frame below, I would like to sort by column "x" then by column "y":

df <- data.frame(x = c("A", "D", "A", "C", "D"), y = c(8, 3, 9, 9, 8),
      z = c(1, 1, 1, 2, 1))

I cannot specify columns like:

library(dplyr)
arrange(df,x,y)

because my code must be able to order data frame by a variable number of columns (for this data frame: 1, 2 or 3 columns).

I tried the following:

columnsOrder = c("x","y")
arrange(df,columnsOrder)

But it doesn't work.

Chris
  • 33
  • 3
  • Or maybe `arrange(df, !!as.symbol(columnsOrder))`, not sure how it works just copied from https://stackoverflow.com/questions/49015759/arrange-by-custom-order-in-dplyr-using-dynamic-column-names – Ronak Shah Oct 03 '18 at 07:53
  • 1
    Question: Are you limited for some reason to _only_ using the `arrange` function to sort your data frame? – Tim Biegeleisen Oct 03 '18 at 07:57
  • 1
    @TimBiegeleisen from what I understand, OP wants to use a `vector` containing variables to be used for the sorting without having to specify the variables directly in the call to `arrange`, so question a bit more specific than just order/sort, but maybe the answer is still somewhere in the Q&A – Cath Oct 03 '18 at 07:57
  • Thanks @RonakShah `arrange(df, !!as.symbol(columnsOrder))` works. – Chris Oct 03 '18 at 09:37
  • @TimBiegeleisen I'm not limited to `arrange` function but I got issue to install `Deducer` library in order to use `sortData`. – Chris Oct 03 '18 at 09:41
  • 1
    @Cath You perfectly understand the problem. Thanks, your solution is more elegant or simple. – Chris Oct 03 '18 at 09:48

1 Answers1

1

If you want to "reach" the real columns, you can either use function arrange_ (which is deprecated now...) instead of arrange, with parameter .dots to pass your vector of variable names:

arrange_(df, .dots=columnsOrder)
#  x y z
#1 A 8 1
#2 A 9 1
#3 C 9 2
#4 D 3 1
#5 D 8 1

Or you can do it with rlang::syms and quasiquotation to create names from your strings vector:

df %>% arrange(!!! rlang::syms(columnsOrder))
Cath
  • 22,843
  • 4
  • 45
  • 80