10.1 order()
: Sorting data
To sort the rows of a dataframe according to column values, use the order()
function. The order()
function takes one or more vectors as arguments, and returns an integer vector indicating the order of the vectors. You can use the output of order()
to index a dataframe, and thus change its order.
Let’s re-order the pirates
data by height from the shortest to the tallest:
# Sort the pirates dataframe by height
pirates <- pirates[order(pirates$height),]
# Look at the first few rows and columns of the result
pirates[1:5, 1:4]
## id sex age height
## 39 39 female 25 130
## 854 854 female 25 130
## 30 30 female 26 135
## 223 223 female 28 135
## 351 351 female 36 137
By default, the order()
function will sort values in ascending (increasing) order. If you want to order the values in descending (decreasing) order, just add the argument decreasing = TRUE
to the order()
function:
# Sort the pirates dataframe by height in decreasing order
pirates <- pirates[order(pirates$height, decreasing = TRUE),]
# Look at the first few rows and columns of the result
pirates[1:5, 1:4]
## id sex age height
## 2 2 male 31 209
## 793 793 male 25 209
## 430 430 male 26 201
## 292 292 male 29 201
## 895 895 male 27 201
To order a dataframe by several columns, just add additional arguments to order()
. For example, to order the pirates
by sex and then by height, we’d do the following:
# Sort the pirates dataframe by sex and then height
pirates <- pirates[order(pirates$sex, pirates$height),]
By default, the order()
function will sort values in ascending (increasing) order. If you want to order the values in descending (decreasing) order, just add the argument decreasing = TRUE
to the order()
function: