# [R] Indices of lowest values in matrix

peter dalgaard pdalgd at gmail.com
Mon Apr 15 14:50:20 CEST 2013

```On Apr 15, 2013, at 14:27 , eliza botto wrote:

> 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

Something like this?

> m
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12]
[1,]  1.2 -2.5  0.4 -0.2  1.4 -1.8 -1.6 -0.1 -0.1  -0.3  -0.2  -0.5
[2,] -0.2 -1.9  0.9  0.0 -1.0  0.2 -0.8  0.7 -1.5  -1.4   1.8   0.4
[3,] -0.9  0.7  0.4 -0.4 -1.6  0.6  1.1 -0.9  2.3   1.0   0.6   0.8
[4,]  1.4 -0.7  0.0 -2.3  0.4  1.0  1.5 -0.8  1.0  -0.1   0.6   0.3
[5,]  0.8 -0.1  1.3 -0.1  1.8 -0.7 -1.4 -0.9  0.6   0.7   0.3   1.4
[6,]  0.4  0.0  0.6 -0.4  0.7 -0.3  1.5  1.6 -1.9  -1.1   0.3   0.6
[7,]  0.5 -0.4 -1.9 -2.4  0.3  0.3  0.1  0.6 -0.9   0.2   0.0   1.4
[8,] -0.2  0.4 -1.4 -0.1 -0.1  1.2  0.2  0.0 -0.9   0.4  -0.3  -2.0
[9,] -0.3  1.0 -0.7  0.9 -1.7 -0.8  1.8 -0.4  0.1   0.2   0.4   1.2
[10,] -0.6  0.7 -0.1  0.3  1.4  0.7 -0.1  1.0 -0.8   0.9   0.0  -0.6
[11,] -0.1  0.2  0.4 -0.3 -1.5 -0.2  1.0  1.0 -0.5  -0.4  -1.2   1.1
[12,] -1.1  0.3  0.7 -0.9  0.2 -1.0  0.2 -1.1 -1.2  -0.3   0.8  -0.9

> apply(m,1,order)[1:5,]
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12]
[1,]    2    2    5    4    7    9    4   12    5     9     5     9
[2,]    6    9    1    8    8   10    3    3    6     1    11     1
[3,]    7   10    8    2    6    4    9    9    3    12     9     8
[4,]   12    5    4   10    2    6    2   11    8     3    10     6
[5,]   10    7    3    3    4    2   11    1    1     7     4     4

--
Peter Dalgaard, Professor
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Email: pd.mes at cbs.dk  Priv: PDalgd at gmail.com

```