Dear Arun,
Thankyou very much. it worked out
:D
Elisa
> Date: Mon, 15 Apr 2013 07:19:40 -0700
> From: smartpink111@yahoo.com
> Subject: Re: [R] Indices of lowest values in matrix
> To: eliza_botto@hotmail.com
>
> Hi Elisa,
> I am not sure how you got that values for the first rows of mat1. Is it
from other dataset
> set.seed(30)
> mat1<- matrix(sample(1:50,12*12,replace=TRUE),ncol=12)
> mat1[1,]
> # [1] 5 28 13 4 47 3 30 16 3 39 42 9
> #So, here the lowest 5 values are 3,3,4,5,9
>
> sort(mat1[1,],index.return=TRUE)
> #$x
> #[1] 3 3 4 5 9 13 16 28 30 39 42 47
>
> #$ix
>
> #[1] 6 9 4 1 12 3 8 2 7 10 11 5
>
> From my previous solution:
> sapply(seq_len(nrow(mat1)),function(i) {order(mat1[i,])})[1:5,1]
> #[1] 6 9 4 1 12 #which are the column numbers.
> A.K.
>
>
> ________________________________
> From: eliza botto <eliza_botto@hotmail.com>
> To: "smartpink111@yahoo.com" <smartpink111@yahoo.com>
> Sent: Monday, April 15, 2013 10:09 AM
> Subject: RE: [R] Indices of lowest values in matrix
>
>
>
>
> Thankyou very much.
>
>
> one additional question is that what if i am interest to find that along
the row 1 which are 5 lowest values? more precisely, for row 1 of mat1 the five
lowest values are
>
> 3,5,8,19,21
>
> therefore, i should have
>
> 1,2,8,5,4 (the column numbers which contain these values)
>
> Thanks in advance
>
> Elisa
>
>
>
> > Date: Mon, 15 Apr 2013 06:53:32 -0700
> > From: smartpink111@yahoo.com
> > Subject: Re: [R] Indices of lowest values in matrix
> > To: eliza_botto@hotmail.com
> > CC: r-help@r-project.org
> >
> > 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@hotmail.com>
> > To: "r-help@r-project.org" <r-help@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@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.
> >
[[alternative HTML version deleted]]