Takes "n" values from the head or tail of a sorted vector. Utilizes the "partial" argument from sort for increased efficiency.

sortEnds(invec, n, where = "head")

Arguments

invec

The input vector

n

The number of values desired

where

Either "head" or "tail".

Value

A sorted vector of length = n

Note

The "tail" approach may not be consideraby faster than the standard approach.

Author

Ananda Mahto

Examples

set.seed(1) x <- sample(300, 45, TRUE) sortEnds(x, 3)
#> [1] 13 14 20
sortEnds(x, 3, "tail")
#> [1] 296 298 299
## Compare with head(sort(x), 3)
#> [1] 13 14 20
tail(sort(x), 3)
#> [1] 296 298 299