Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 277
user2560984 Avatar asked Jul 12 '13 16:07

user2560984


3 Answers

order(R, decreasing=TRUE)[1:N]
like image 125
Hong Ooi Avatar answered Nov 16 '22 05:11

Hong Ooi


Here is an alternative:

N <- 2
v <- c(3,  9, 11,  18,  5)
tail(order(v), N)
# [1] 3 4
like image 28
QuantIbex Avatar answered Nov 16 '22 07:11

QuantIbex


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]]
}
like image 6
Epimetheus Avatar answered Nov 16 '22 07:11

Epimetheus