Sorting Data Frames by Columns in R
Overview
Sorting data in R can be easily accomplished by using the sort()
function, which by default only sorts vectors. However, in practice, dealing with numerous categories in a data frame often necessitates the ability to sort based on columns as well.
Code
x<-c(pi,3,99,0,-1)
order(x)
x[order(x)]
head(iris)
head(iris[order(iris$Petal.Length)])
This demonstrates how computing the ‘position’ vector in ascending order from the beginning for every component of the vector can be achieved, similar to the operation of the sort()
function.
The difference with this approach, compared to general sorting, is that it specifically refers to the row numbers, making it applicable to data frames. Consider the following example.
The example above illustrates the sorting of iris
data based on Petal.Length
. By obtaining a vector that can be sorted by Petal.Length
through the order()
function and then referencing iris
data accordingly, a new data frame sorted by columns is effectively obtained.