Sort a dataframe by myltiple columns (large set of columns) in R

If we have multiple columns, use do.call with order on the subset of dataset

data.d[do.call(order, data.d[,3:7]),]

Or with arrange

library(dplyr)
data.d %>%
      arrange(across(all_of(3:7)))

The reason for getting that output is because

order(data.d[,3:7])
#[1]  2  4  5  6  7 10 12 13 14 15 16 17 33 34 36 37 38 39  1  3  8  9 11 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 35 40

it unlist the data into a vector and now we have the order for the elements of the vector instead of the order among the columns

order(unlist(data.d[3:7]))
#[1]  2  4  5  6  7 10 12 13 14 15 16 17 33 34 36 37 38 39  1  3  8  9 11 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 35 40

The index values showed have values greater than the nrow of the dataset. So, for each element that is greater than those, it will return a NA row because that row doesn’t exist in the data

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top