sort
Basics
sort
is a function that allows you to arrange an object into ascending or descending order. The relevant arguments include:
-
x
: mandatory; an object that contains a numeric, complex, logical, or character vector. -
partial
: if notNULL
, then a vector with indices that will have the correct sorted value. -
decreasing
:TRUE
to yield decreasing vector,FALSE
to yield increasing vector. Default isFALSE
.
In most situations, you will want to fully sort your object, which is straightforward.
For partial sorting, you can select indices to display the correct sorted value at those indices, though no other elements are guaranteed to be sorted.
Partial sorting does guarantee two things:
Again, the left and right subsections surrounding an index are not sorted. Partial sorting is quicker than regular sorting due to fewer elements being sorted, which can save considerable time when looking for specific placement in large data sets. |
Examples
Given a vector, arrange it in ascending order.
Click to see solution
x <- c(1,3,2,10,4)
sort(x)
[1] 1 2 3 4 10
What is the descending order of the previous vector?
Click to see solution
x <- c(1,3,2,10,4)
sort(x, decreasing = TRUE)
[1] 10 4 3 2 1