Ordering multiple vectors dynamic


Is there an equivalent to order() in base R that works with data.frames that have an arbitrary number of columns

For example say I have a dataframe

df = data.frame(
  col1 = c(1, 1, 2, 3),
  col2 = c(2, 1,3, 4)

# I could get a heirarchical order using the `order` function
order(df$col1, df$col2)
#> [1] 2 1 3 4

# But what if i want to turn this into a function where we don't need to know the number of columns in advanced?
order_dataframe <- function(df){
    # what goes here to produce the same output as the above call to `order`?

Created on 2022-09-12 by the reprex package (v2.0.1)

order_dataframe <- function(df){
1 Like

Thanks @nirgrahamuk! Thats exactly what I was after

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.