Search code examples
rlistsortingindexingmax

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.


Solution

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