What is the order() function in R?
Overview
The order() function in R is used to return a permutation that simply orders or rearranges a sequence of numeric, complex, character, or logical vectors in ascending or descending order by their index positions.
Syntax
order(…, na.last = TRUE, decreasing = FALSE)
Syntax for the order() function in R
Parameters
The order() function takes the following parameter values:
...: This is a sequence of numeric, complex, character, or logical vectors of the same length to be ordered in ascending or descending order. This is a required parameter.na.last: This takes a boolean value (TRUEorFALSE). IfTRUE, the missing value in the input data is put last. IfFALSE, they are removed first andNAvalues are also removed. This is an optional parameter.decreasing: This takes a boolean value indicating if the sort order should be in ascending (increasing) or descending (decreasing) order. This is an optional parameter.
Example
# creating a numerical objecta <- c(100, 5, 2, 8, 10, 1, 0.5)# sorting increasinglyorder(a, decreasing=TRUE)# sorting decreasignlyorder(a, decreasing = FALSE)
Explanation
- Line 2: We create a numerical object,
a. - Line 5: We sort the values of the object in ascending order by their index position using the
order()function. - Line 8: We sort the values of the object in descending order by their index position using the
order()function.