#/ Data:
df <- 
data.frame(order=seq(1,6),
           class=rep(c("a", "b"), each=3),
           value=c(5,6,4,10,7,18))

#/ Base R ::: https://stackoverflow.com/questions/1296646/how-to-sort-a-dataframe-by-multiple-columns
df[with(df, order(class, value)), ]

#/ dplyr ::: https://dplyr.tidyverse.org/reference/arrange.html
library(dplyr)
df %>% arrange(class, value)

As usual the base solution is much faster compared to dplyr (tidyverse). Still, with multiple filtering conditions and with operations up- and downstream of this command the tidyverse syntax / verbs can be more convenient to write and easier to read, so exactly what the tidyverse has been developed for (it has not been developed for speed, this is a common misconception from what I understand).

library(microbenchmark)
base=function(x) df[with(df, order(class, value)),]
tidy=function(x) df %>% arrange(class, value)
microbenchmark(base=base(), tidy=tidy())

Unit: microseconds
 expr      min        lq       mean    median       uq      max neval
 base   59.125   71.5165   96.27901   79.3505   88.886 1680.796   100
 tidy 1611.210 1782.8240 1941.95609 1887.5755 1937.872 7368.565   100



Source link