sort, order & rank Functions in R (Example)

The following R code shows how to apply the sort(), order(), and rank() functions in the R programming language.

Example Data

vec <- c(2, 5, 1, 4, 3)  # Create numeric example vector

Example for the sort Function

The sort functions sorts a numeric or character vector from the smallest to the largest value:

sort(vec)                # Application of sort function
# 1 2 3 4 5

Example for the order Function

The order function returns the ordering of a numeric or character vector:

order(vec)               # Application of order function
# 3 1 5 4 2

Example for the rank Function

The rank function returns the sample ranks of the values in a vector:

rank(vec)                # Application of rank function
# 2 5 1 4 3

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed