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.