Compute the k
-th smallest value in a dataset,
or find which entry in a dataset is the k
-th smallest.
orderstats(x, k, decreasing = FALSE)
orderwhich(x, k, decreasing = FALSE)
orderstats
returns a vector of the same kind as x
,
with the same length as k
.
orderwhich
returns an integer vector
with the same length as k
.
Data whose order statistics will be computed. A numeric vector.
Rank. An integer, or vector of integers.
Logical value specifing whether a rank of 1
is assigned to the highest value (decreasing=TRUE
)
or the lowest value (decreasing=FALSE
, the default).
Adrian Baddeley Adrian.Baddeley@curtin.edu.au.
These are low-level functions for efficiently computing order statistics:
orderstats(x, k)
returns the k
-th smallest value in x
,
and orderwhich(x, k)
returns the position of the
k
-th smallest value in x
.
Given a dataset of values \(x_1, \dots, x_n\), the order statistic of rank \(k\) is the \(k\)-th smallest value in the dataset. The order statistic of rank 1 is the smallest value, and the order statistic of rank \(n\) is the largest value. The order statistic of rank \(k\) is denoted \(x_{[k]}\).
The full sequence of order statistics $$ x_{[1]} \le x_{[2]} \le \dots \le x_{[n]} $$ can simply be obtained by sorting the original values into increasing order.
The command orderstats(x, k)
is equivalent to
sort(x)[k]
; it calculates the
k
-th smallest value in x
.
The command orderwhich(x, k)
is equivalent to
order(x)[k]
. It identifies the position of the
k
-th smallest value in x
, that is, it returns the
index j
such that x[j]
is the k
-th smallest value
in x
.
The functions orderstats
and orderwhich
are more
efficient than using sort
and order
when it is only desired to calculate a few of the
order statistics (for example, only the smallest and second-smallest
values in the dataset).
x <- runif(10)
orderstats(x, 2)
sort(x)[2]
orderwhich(x, 2:3)
order(x)[2:3]
Run the code above in your browser using DataLab