[R] Sorted index of values in matrix
Jeff Newmiller
jdnewm|| @end|ng |rom dcn@d@v|@@c@@u@
Fri Apr 15 17:33:22 CEST 2022
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.
More information about the R-help
mailing list