[R] Sorted index of values in matrix

Eliza Botto e||z@_botto @end|ng |rom out|ook@com
Wed Apr 20 11:14:02 CEST 2022


Thankyou for your assistance. 🙂
________________________________
From: R-help <r-help-bounces using r-project.org> on behalf of Eliza Botto <eliza_botto using outlook.com>
Sent: Friday 15 April 2022 23:51
To: Bert Gunter <bgunter.4567 using gmail.com>; Jeff Newmiller <jdnewmil using dcn.davis.ca.us>
Cc: R-help <r-help using r-project.org>
Subject: Re: [R] Sorted index of values in matrix

Thankyou very much. It worked.


Eliza
________________________________
From: Bert Gunter <bgunter.4567 using gmail.com>
Sent: Friday 15 April 2022 17:00
To: Jeff Newmiller <jdnewmil using dcn.davis.ca.us>
Cc: R-help <r-help using r-project.org>; Eliza Botto <eliza_botto using outlook.com>
Subject: Re: [R] Sorted index of values in matrix

...
But Ivan's solution -- which I had to think about -- is better, and it
also generalizes to arbitrary arrays:

arrayInd(order(qq), .dim = dim(qq))

Bert Gunter


On Fri, Apr 15, 2022 at 9:25 AM Bert Gunter <bgunter.4567 using gmail.com> wrote:
>
> ... and here is another version of Jeff's solution using the built in
> row() and col() functions instead of explicitly creating the indices
> (but his version generalizes to arbitrary arrays; this does not):
>
>  qq <- matrix(12:7, nrow =3) ## start with a non-sorted matrix
> > qq
>      [,1] [,2]
> [1,]   12    9
> [2,]   11    8
> [3,]   10    7
> > sort(qq)
> [1]  7  8  9 10 11 12 ## the sorted values
>
> > o <- order(qq)
> > cbind(row(qq)[o], col(qq)[o])
> ## matrix of (row, col) indices of sorted values in original unsorted matrix
>      [,1] [,2]
> [1,]    3    2
> [2,]    2    2
> [3,]    1    2
> [4,]    3    1
> [5,]    2    1
> [6,]    1    1
>
> Cheers,
> Bert Gunter
>
> On Fri, Apr 15, 2022 at 8:33 AM Jeff Newmiller <jdnewmil using dcn.davis.ca.us> wrote:
> >
> > Please stop posting in HTML format.
> >
> > I think your specified result coordinates are in (col,row) order, which is not normal for R. The solution below yields (row,col) order.
> >
> > qq <- structure(1:6, .Dim = 3:2, .Dimnames = list(c("1", "2", "3"), c("1", "2")))
> > pos <- expand.grid( r = seq.int( nrow( qq ) )
> >                   , c = seq.int( ncol( qq ) ) )
> > qq #'confirm source layout is column major
> > pos[ order( qq ), ] # (row,col)
> > pos[ order( qq ), 2:1 ] # (col,row)
> >
> >
> > On April 15, 2022 7:53:36 AM PDT, Eliza Botto <eliza_botto using outlook.com> wrote:
> > >deaR useRs,
> > >
> > >I have this following simple dataset
> > >
> > >> dput(qq)
> > >
> > >structure(1:6, .Dim = 3:2, .Dimnames = list(c("1", "2", "3"),
> > >    c("1", "2")))
> > >
> > >I want to extract row and column index of the sorted values of this matrix
> > >
> > >For example,
> > >
> > >sort(qq) command will execute the sorted values of this matrix as (1 2 3 4 5 6). Instead of values, I want the row and column names of these sorted values as (1,1), (1,2), (1,3), (2,1), (2,2), (2,3)
> > >
> > >I thankyou very much in advance for your help.
> > >
> > >Eliza
> > >
> > >
> > >       [[alternative HTML version deleted]]
> > >
> > >______________________________________________
> > >R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> > >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.
> >
> > --
> > Sent from my phone. Please excuse my brevity.
> >
> > ______________________________________________
> > R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> > 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]]

______________________________________________
R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
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]]



More information about the R-help mailing list