partial_argsort: Extract indecies corresponding to the largest elements

View source: R/RcppExports.R

partial_argsortR Documentation

Extract indecies corresponding to the largest elements

Description

Extracts the indecies of the n largest elements of the input This procedure is equivalent to order(x, decreasing = TRUE)[1:n_elements], but is much faster and avoids the overhead of sorting discarded elements. This function is useful for extracting the rows in a data frame having the largest values in one of the columns.

Usage

partial_argsort(x, n_elements)

Arguments

x

Numeric vector, the vector containing the numbers to sort.

n_elements

Integer scalar, the number of indecies to return.

Value

Numeric vector, the indecies of the largest elements (in sorted order) in x.

Examples

x <- c(10L,5L,-2L,12L,15L)
max_indecies <- partial_argsort(x,3L)
max_indecies
x[max_indecies]
order(x)[1:3]
mtcars[partial_argsort(mtcars$hp,5L),]

AlmaasLab/csdR documentation built on Dec. 9, 2022, 6:22 a.m.