This function returns the kth smallest, resp. largest values from a vector x.
Usage
Large(x, k = 1, unique = FALSE, na.rm = FALSE)
Arguments
x
a numeric vector
k
an integer >0 defining how many extreme values should be returned. Default is k = 1. If k > length(x), all values will be returned.
unique
logical, defining if unique values should be considered or not. If this is set to TRUE a list with the extreme value and its frequency is returned. Default is FALSE.
na.rm
logical, indicating whether NA values should be stripped before the computation proceeds. Defaults to FALSE.
Value
either a vector with the k most extreme values, if unique is set to FALSE
or a list, containing the k most extreme values and their respective frequency.
Details
There are several points discussed about that out there. This implementation uses the function sort(partial), which is'nt the fastest, but a fairly fast one.
x <- sample(1:10, 1000, rep=TRUE)
Large(x, 3)
Large(x, k=3, unique=TRUE)
# works fine up to 1 Mx <- runif(1000000)
Small(x, 3, unique=TRUE)
Small(x, 3, unique=FALSE)