partial_argsort | R Documentation |
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.
partial_argsort(x, n_elements)
x |
Numeric vector, the vector containing the numbers to sort. |
n_elements |
Integer scalar, the number of indecies to return. |
Numeric vector, the indecies of the largest elements (in sorted order) in
x
.
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),]
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.