partial_argsort {csdR} | R 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),]

[Package csdR version 1.0.0 Index]