简体   繁体   中英

find the index of top n elements in a vector in order [R]

I have a matrix of type numeric, with dim 10000 * 50. Now I want to find the index of top 5 elements in every row in the order of their values. eg a sample might look like :

set.seed(2)
v1 <- matrix(runif(20 , 0 ,20 ) , 2 ,10)
v1
#          [,1]      [,2]     [,3]     [,4]     [,5]      [,6]      [,7]      [,8]      [,9]    [,10]
#[1,]  3.697645 11.466527 18.87679  2.58318  9.36037 11.053481 15.210266  8.105644 19.527970 8.896185
#[2,] 14.047481  3.361038 18.86950 16.66898 10.99967  4.777895  3.616402 17.070969  4.516509 1.499588

Then I want the output to look like :

#[1,]    9    3    7    2    6
#[2,]    3    8    4    1    5

I could find only this question , which explains how to find top n elements, but not in the order of values.

apply() is perfect for row-wise operations on matrices. You could do

t(apply(v1, 1, function(x) order(-x)[1:5]))
#      [,1] [,2] [,3] [,4] [,5]
# [1,]    9    3    7    2    6
# [2,]    3    8    4    1    5

This runs the order() function row-wise down the matrix v1 then takes the first five values for each one, transposing the result since you specify rows not columns.

This can also be done with data.table after melting into 'long' format, grouped by 'Var1', we get the order of 'value'

library(reshape2)
library(data.table)
setDT(melt(v1))[, head(order(-value),5), Var1]
#    Var1 V1
#1:    1  9
#2:    1  3
#3:    1  7
#4:    1  2
#5:    1  6
#6:    2  3
#7:    2  8
#8:    2  4
#9:    2  1
#10:   2  5

Or using base R

ave(-v1, row(v1), FUN = order)[,1:5]
#     [,1] [,2] [,3] [,4] [,5]
#[1,]    9    3    7    2    6
#[2,]    3    8    4    1    5

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM