I have a list of floats in R. For a given integer, N, I want to find the indices of the largest N values in my list. So for example, if N is 2, I want to find the indices of the two largest values in my list. How do I do this?
I cannot reorder my list. This is why I need the indices.
order(R, decreasing=TRUE)[1:N]
Here is an alternative:
N <- 2
v <- c(3, 9, 11, 18, 5)
tail(order(v), N)
# [1] 3 4
All of the other current answers require a call to order
that will run in O(M log M) time. If N is much smaller than the total number of elements M, a quicker way is to partially sort the list and then to extract the indices greater than or equal to the N'th largest. This has O(M + N log N) running time and will be much quicker for large M.
v <- list(1,7,4,3,9,1,2,3,0,1,2)
vec <- unlist(v)
N <- 3
partial <- length(v) - N + 1
Nth <- sort(vec, partial = partial)[partial]
indexes <- which(vec >= Nth)
vec[indexes]
Note this will not deal with ties in the list. There is a longer discussion here.
It is idiomatic to store numeric data in a vector not a list. Hence the call to unlist
above.
As a function, this can be implemented like so:
maxn <- function(x, n) {
partial <- length(x) - n + 1
x[x >= sort(x, partial = partial)[partial]]
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With