[R] sort adjacency matrix

Bert Gunter gunter.berton at gene.com
Mon Apr 6 20:15:23 CEST 2015


Not quite, David.

If I understand the OP's query, he wants the ties to be broken by the
"lexicographic" order (with apologies if I have misused this term) of
the 1's within the rows. Makes things a bit more interesting.

Have at it!

Cheers,
Bert

Bert Gunter
Genentech Nonclinical Biostatistics
(650) 467-7374

"Data is not information. Information is not knowledge. And knowledge
is certainly not wisdom."
Clifford Stoll




On Mon, Apr 6, 2015 at 11:09 AM, David L Carlson <dcarlson at tamu.edu> wrote:
> The answer depends on what kind of matrix/data frame you have. That is why we encourage people to use dput() to create a copy of the sample data in their email. Some combination of order() function the rowSums() function will probably get you what you want. For example,
>
> dat[order(rowSums(dat=="1"), decreasing=TRUE),]
>
> or
>
> dat[order(rowSums(dat), decreasing=TRUE),]
>
> or
>
> dat[order(rowSums(dat, na.rm=TRUE), decreasing=TRUE),]
>
> Note that the order is not unique since there are ties in the number of 1s.
>
> -------------------------------------
> David L Carlson
> Department of Anthropology
> Texas A&M University
> College Station, TX 77840-4352
>
>
> -----Original Message-----
> From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Ragia Ibrahim
> Sent: Monday, April 6, 2015 12:18 PM
> To: r-help at r-project.org
> Subject: [R] sort adjacency matrix
>
> Dear group
> i have the following matrix
>
> 1  . . 1 . . 1 . . . .
> 2  . . . . . . 1 . . .
> 3  1 . . . 1 . . 1 . 1
> 4  . . . . . 1 . . . .
> 5  . . 1 . . . . . . 1
> 6  1 . . 1 . . . . 1 .
> 7  . 1 . . . . . 1 . .
> 8  . . 1 . . . 1 . . 1
> 9  . . . . . 1 . . . 1
> 10 . . 1 . 1 . . 1 1 .
>
> I want to sort it according to ones in each row ascending (where max number of ones first)
>
> to be as follow
>
> 3  1 . . . 1 . . 1 . 1
> 10 . . 1 . 1 . . 1 1 .
> 6  1 . . 1 . . . . 1 .8  . . 1 . . . 1 . . 11  . . 1 . . 1 . . . .5  . . 1 . . . . . . 17  . 1 . . . . . 1 . .9  . . . . . 1 . . . 12  . . . . . . 1 . . .4  . . . . . 1 . . . .
>
> how can I do this in R
> thanks in advance
>
>         [[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.
>
> ______________________________________________
> 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.



More information about the R-help mailing list