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.

6
  • Or maybe arrange(df, !!as.symbol(columnsOrder)), not sure how it works just copied from stackoverflow.com/questions/49015759/… Commented Oct 3, 2018 at 7:53
  • 1
    Question: Are you limited for some reason to only using the arrange function to sort your data frame? Commented Oct 3, 2018 at 7: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 Commented Oct 3, 2018 at 7:57
  • Thanks @RonakShah arrange(df, !!as.symbol(columnsOrder)) works. Commented Oct 3, 2018 at 9:37
  • 1
    @Cath You perfectly understand the problem. Thanks, your solution is more elegant or simple. Commented Oct 3, 2018 at 9:48

1 Answer 1

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))
Sign up to request clarification or add additional context in comments.

2 Comments

Unfortunately, I tested it with another data frame: df <- data.frame(x = c("A", "D", "A", "C", "D"), y = c(8, 8, 9, 9, 3), z = c(1, 1, 1, 2, 1)) and it was just sorted by first column.
Hi @Chris, indeed get wasn't going past the first variable, I edited my answer, it should work now

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.