R: Sorting a Vector

sort {base} | R Documentation

## Sorting a Vector

### Description

Sort a numeric or complex vector (partially) into ascending order.

### Usage

    sort(x, partial = NULL, na.last = NA)
    is.unsorted(x, na.rm = FALSE)

### Arguments

`x` | a numeric or complex vector.
`partial` | a vector of indices for partial sorting.
`na.last` | for controlling the treatment of `NA`s. If `TRUE`, missing values in the data are put last; if `FALSE`, they are put first; if `NA`, they are removed.
`na.rm` | logical. Should missing values be removed?

### Details

If `partial` is non `NULL`, it is taken to contain indexes of elements of `x` which are to be placed in their correct positions by partial sorting. After the sort, the values specified in `partial` are in their correct position in the sorted array. Any values smaller than these values are guaranteed to have a smaller index in the sorted array and any values which are greater are guaranteed to have a bigger index in the sorted array.

`is.unsorted` returns a logical indicating if `x` is sorted increasingly, i.e. `is.unsorted(x)` is true if `any(x != sort(x))` (and there are no `NA`s).

### See Also

`order`, `rank`.

### Examples

    data(swiss)
    x <- swiss$Education[1:25]
    x; sort(x); sort(x, partial = c(10, 15))
    median # shows you another example for `partial'

    stopifnot(!is.unsorted(sort(x)),
              !is.unsorted(LETTERS),
               is.unsorted(c(NA,1:3,2), na.rm = TRUE))

[Package Contents]