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 |
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 |
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 |
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 |
rank(vec) # Application of rank function # 2 5 1 4 3