Bogdan Bogdan - 17 days ago 5
R Question

How can I sort by multiple columns in R if the column names are stored in a vector?

So I am trying to tidy up some data, and want to arrange my table based on several columns. So if I have a table

a <- (1:5)
b <- (6:10)
c <- (11:15)
d <- (16:20)
df <- as.data.frame(cbind(a,b,c,d))


I need to sort by column a, then by column b, but I have to take these values from a vector.

x <- c(a,b)


Currently I am using order or arrange and inserting the column names manually, so something like:

df <- arrange(df, a, b)


But ideally I would not have to insert them manually, and be able to take them from a vector, which might change depending on certain other parameters.

I tried something like

df <- df[,with(df, order(x))]


Or

df <- df[order(df[,1:2]),]


But it keeps messing up the table. The data frame I'm working with is quite big, and the columns might differ depending on a circumstance, so I want to know if it is possible inserting them manually each time.

Thank you.

Answer

We need to have a vector of column names

x <- names(df)[1:2]

and then use arrange_

df %>%
   arrange_(x)
Comments