order
Vector of Indices That Sort Data

Description

Returns a numeric vector of integers that corresponds to the sort order of the elements in the input. The order function allows multiple inputs to break ties. The sort.list function only allows a single input but allows for partial ordering.

Usage

order(..., na.last = TRUE, decreasing = FALSE)
sort.list(x, partial = NULL, na.last = TRUE, decreasing = FALSE,
    method = c("shell", "quick", "radix"))

Arguments

... any number of vectors. The vectors can be a mix of numeric, character, and complex. All vectors should have the same length. Missing values (NAs) are allowed.
x a vector. Missing values (NAs) are allowed. For numeric vectors, infinite values are also allowed.
na.last a logical value.
  • If TRUE (the default), missing values (NAs) are placed last.
  • If FALSE, NAs are placed first.
  • If na.last=NA, NAs are discarded.
decreasing
  • For sort.list, a logical value.
  • For order, a logical vector with an element for each vector in ... (If this is the wrong length, then it is replicated to the required length.)
    • If TRUE, then the elements have a descending sort order.
    • If FALSE (the default), the elements have an ascending order.
partial a vector of indices (in ascending order) into data for partial sorting.
method by default, the vector is sorted using a stable sort algorithm. If method = "quick" is specified, then an unstable (and faster) sort algorithm is used. Specifying any other method is treated like the default. In particular, the "shell" and "radix" methods are present only for R compatibility and do not imply using shell sort or radix sort.

Details

These functions do not return the input data sorted. Use the sort function for that operation.
If the input contains missing values, the behavior is controlled by na.last.
Ordering is based on the first argument. For the order function, values of the second argument break ties in the first, and so on.
Character data are ordered according to the ASCII collating sequence, where digits precede upper-case letters, which precede lower-case letters. The position of other characters is not intuitive.
Complex data are ordered according to the real part, and the imaginary part is used to break ties.
By default, ordering of non-atomic data is not allowed, because the definition of ordering is too vague. However, for classed data, the generic xtfrm function is called to provide an ordering. If it succeeds, the ordering also succeeds.
This function is often used in conjunction with subscripting for sorting several parallel arrays.
Value
returns an integer vector containing the indices of the sorted data elements. That is, the first integer is the subscript of the smallest data element (if decreasing = FALSE). By default, the returned value has the same length as the input, but it might be shorter if na.last=NA.
See Also
rank, sort, rev, xtfrm.
Examples
x <- c(4, NA, -Inf, 3, 5, 7)  # create sample object
# order says that the smallest element in x (-Inf) is found in 
# position 3, the next largest in position 4, etc, and NA is 
# considered the largest value.
order(x, na.last=TRUE)
# [1] 3 4 1 5 6 2
sort.list(x, na.last=TRUE)  # same as order result

# Here the NA value is removed. order(x, na.last=NA) # [1] 3 4 1 5 6 sort.list(x, na.last=NA) # same as order result

x[order(x, decreasing=TRUE)] # x sorted in decreasing order

x100 <- sample(1:100, 100) # 1 to 100 permuted sl <- sort.list(x100, partial=c(1:3, 98:100)) sl[c(1:3, 98:100)] # indices of 3 smallest and 3 largest values of x100

x100[sl[c(1:3, 98:100)]] # [1] 1 2 3 98 99 100

# Use order to sort a data frame: xdf <- data.frame(Age=sample(18:50, 20, rep=TRUE), Gender=sample(c("F", "M"), 20, rep=TRUE), Height=sample(58:74, 20, rep=TRUE)) oo <- order(xdf$Gender, xdf$Age, decreasing=c(FALSE,TRUE)) xdf[oo, ] # sorted by Gender, breaking ties by decreasing Age

Package base version 6.1.4-13
Package Index