[英]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. 我有一个数字类型的矩阵,昏暗的10000 *50。现在,我想按值的顺序在每行中找到前5个元素的索引。 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. 我只能找到这个问题 ,它说明了如何找到前n个元素,但没有按值的顺序。
apply()
is perfect for row-wise operations on matrices. apply()
非常适合矩阵上的按行运算。 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. 这会沿矩阵
v1
逐行运行order()
函数,然后为每个v1
获取前五个值,因为您指定的是行而不是列,所以会转置结果。
This can also be done with data.table
after melting
into 'long' format, grouped by 'Var1', we get the order
of 'value' 这也可以用做
data.table
后melting
成“长”格式,通过“VAR1”分组,我们得到的order
“价值”
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
或使用
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.