[R] Indices of lowest values in matrix
arun
smartpink111 at yahoo.com
Mon Apr 15 15:53:32 CEST 2013
Hi,
TRy this:
set.seed(30)
mat1<- matrix(sample(1:50,12*12,replace=TRUE),ncol=12)
mat1
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12]
[1,] 5 28 13 4 47 3 30 16 3 39 42 9
[2,] 25 44 15 21 9 5 32 5 4 33 30 21
[3,] 19 12 24 2 7 37 36 3 13 9 33 33
[4,] 22 47 13 14 9 29 20 3 1 25 14 29
[5,] 16 13 41 22 15 39 50 44 10 50 32 32
[6,] 8 42 24 32 21 31 45 32 41 44 22 31
[7,] 45 31 31 39 4 25 1 19 28 16 20 48
[8,] 12 22 12 25 45 10 5 7 31 27 18 30
[9,] 49 8 25 10 6 30 8 29 14 10 48 44
[10,] 8 30 31 35 4 50 25 33 7 15 17 38
[11,] 4 2 15 40 30 49 44 14 3 33 41 12
[12,] 20 17 34 14 43 2 40 50 42 42 23 20
#If you need the lowest values:
sapply(seq_len(nrow(mat1)),function(i) {x1<- mat1[i,]; x1[order(x1)][1:5]})
# [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12]
#[1,] 3 4 2 1 10 8 1 5 6 4 2 2
#[2,] 3 5 3 3 13 21 4 7 8 7 3 14
#[3,] 4 5 7 9 15 22 16 10 8 8 4 17
#[4,] 5 9 9 13 16 24 19 12 10 15 12 20
#[5,] 9 15 12 14 22 31 20 12 10 17 14 20
#indices
sapply(seq_len(nrow(mat1)),function(i) {order(mat1[i,])})[1:5,]
# [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12]
#[1,] 6 9 4 9 9 1 7 7 5 5 2 6
#[2,] 9 6 8 8 2 5 5 8 2 9 9 4
#[3,] 4 8 5 5 5 11 10 6 7 1 1 2
#[4,] 1 5 10 3 1 3 8 1 4 10 12 1
#[5,] 12 3 2 4 4 6 11 3 10 11 8 12
A.K.
----- Original Message -----
From: eliza botto <eliza_botto at hotmail.com>
To: "r-help at r-project.org" <r-help at r-project.org>
Cc:
Sent: Monday, April 15, 2013 8:27 AM
Subject: [R] Indices of lowest values in matrix
Dear R users,Sorry for such a basic question. I really need to know that how can i pick the indices of 5 lowest values from each row of a matrix with dimensions 12*12??Thank you very much in advance
Elisa
[[alternative HTML version deleted]]
______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
More information about the R-help
mailing list