How to find the largest N elements in a list in R?
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]]
}
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