Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

sortx

Sorting Vectors


Description

sortx has the same functionality as sort, except that sortx(..., from=from, to=to) is the same as sort[from:to]

Sort a vector or factor into ascending or descending order.

Usage

sortx(x, from=1, to=length(x), decreasing=FALSE, na.last = NA)

Arguments

x

an atomic vector

from,to

sort(..., from=from, to=to) equals sort(...)[from:to]

decreasing

logical. Should the sort sort be increasing or decreasing?

na.last

for controlling the treatment of NAs. If TRUE, missing values in the data are put last; if FALSE, they are put first; if NA, they are removed (see the Notes in sort)

Details

The smaller the difference to-from is compared to the length of x, the faster is sortx compared to sort.

Particularly, sortx(..., from=k, to=k) is much faster than sort(...)[k].

For further details see sort.

Value

vector of length to-from+1.

Author(s)

See Also

Examples

x <- runif(10^6)
k <- 10
system.time(y<-sort(x)[1:k])
system.time(z<-sortx(x, from=1, to=k)) ## much faster
stopifnot(all(y == z)) ## same result

RandomFieldsUtils

Utilities for the Simulation and Analysis of Random Fields

v0.5.3
GPL (>= 3)
Authors
Martin Schlather [aut, cre], Reinhard Furrer [ctb], Martin Kroll [ctb], Brian D. Ripley [ctb]
Initial release

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.