Supose we have a matrix
like this one:
# Set seed
set.seed(12345)
# Generate data.frame
df <- matrix(sample(1:100,100), nrow = 10)
I would like to get the row and column where the first n
highest values are placed.
I know that using which(df == max(df), arr.ind=TRUE)
I get what I want but only for the highest value.
Let's suppose we want the location of the 5 highest values in the matrix. Based on the previous answer, I tried which(aux %in% sort(df, decreasing=T)[1:5], arr.ind = TRUE)
but it did not work.
I also know that using order(df, decreasing=T)
and modulating the results I can get rows and columns I am looking for. Nevertheless, I think it should be a fastest way to get it.
Thank you for your help in advance