...
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 at 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 at
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 at
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 at 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 at 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.