0

I have a vector and I want to find the indices of the k greatest elements, not the elements themselves which I could do with sort. One idea would be to add indices to the values and have a custom sort function that only compares the first elements of pairs (a classical solution to this problem) but surely there has to be a simpler way ? Note that performance isn`t a matter.

2 Answers2

0

First I create a random vector:

vector <- c(1, 3, 6, 2, 7, 8, 10, 4)

Next, you can use the following code which will output the top k elements as x with index ix:

k <- 3

lst <- sort(vector, index.return=TRUE, decreasing=TRUE)
lapply(lst, `[`, lst$x %in% head(unique(lst$x),k))

Output:

$x
[1] 10  8  7

$ix
[1] 7 6 5

As you can see ix gives the index of the top k elements.

Quinten
  • 35,235
  • 5
  • 20
  • 53
  • I see, I didn´t know sort has this index return feature. But could you please elaborate on the purpose of lapply ? What is this bracket for `[` ? – eternalstudent Apr 30 '22 at 08:07
  • @eternalstudent, it returns the output as a list to make sure you see which values are the top elements with a certain index. The `[` is just to subset the dataset based on the index returned from the top elements. Check for more info on `?"["`. – Quinten Apr 30 '22 at 08:11
0

Using rank.

x <- c(1, 3, 6, 2, 7, 8, 10, 4)

seq_along(x)[rank(-x) < 4]
# [1] 5 6 7

If you have ties, the result is this:

x <- c(10, 3, 6, 2, 7, 8, 10, 4)

seq_along(x)[rank(-x) < 4]
# [1] 1 6 7
jay.sf
  • 60,139
  • 8
  • 53
  • 110
  • Thanks for the answer, I selected the other one since it came before, but this answers a slightly different problem : In case of ties your solution doesn´t give the positions of the k greatest elements, but maybe less (in your example it´s the positions of the 2 greatest elements instead of 3). Nevertheless it´s interresting to see both approaches. – eternalstudent Apr 30 '22 at 23:39