user2560984
user2560984

Reputation: 381

How to find the largest N elements in a list in R?

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.

Upvotes: 22

Views: 31704

Answers (5)

Stein Monteiro
Stein Monteiro

Reputation: 311

I think this works as well:

x <- list(1, 2, 2, 4, 5, 5)
which(x %in% max(unlist(x)))

Upvotes: 0

Epimetheus
Epimetheus

Reputation: 1147

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]]
}

Upvotes: 6

eddi
eddi

Reputation: 49448

You probably mean vector and not a list, and here's an example:

v = c(1,7,4,3,9)
v[order(-v)][1:3]
#[1] 9 7 4

Re comment:

order(-v)[1:3]
#[1] 5 2 3

Upvotes: 4

QuantIbex
QuantIbex

Reputation: 2384

Here is an alternative:

N <- 2
v <- c(3,  9, 11,  18,  5)
tail(order(v), N)
# [1] 3 4

Upvotes: 7

Hong Ooi
Hong Ooi

Reputation: 57686

order(R, decreasing=TRUE)[1:N]

Upvotes: 28

Related Questions