Reputation: 1089
I am trying to sort a database by each column in the database. I have tried with the following code
db <- db[order(db[,1],db[,2],db[,3],db[,4],db[,5],db[,6]), ]
and it works perfectly. However I would like to write it in a more efficient way, because I don't know if the database will always have 6 columns. Is there a way to do this more efficiently?
Upvotes: 2
Views: 54
Reputation: 70336
Here are three approaches to do that:
1) base R
data(mtcars)
mtcars <- mtcars[do.call(order, mtcars),]
2) dplyr
data(mtcars)
library(dplyr)
mtcars <- arrange_all(mtcars)
3) data.table
data(mtcars)
library(data.table)
setDT(mtcars)
setorderv(mtcars, names(mtcars))
Upvotes: 5