Skip to content

sort adjacency matrix

6 messages · Ragia Ibrahim, David L Carlson, Bert Gunter +2 more

#
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
#
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
 		 	   		  

______________________________________________
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.
#
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:
#
Hello,

You should have used ?dput to post your data example.
Since you haven't, I've made up one.

set.seed(4795)
mat <- matrix(sample(0:1, 24, replace = TRUE), nrow = 6)
mat

inx <- order(rowSums(mat), decreasing = TRUE)
mat[inx, ]


Hope this helps,

Rui Barradas

Em 06-04-2015 18:18, Ragia Ibrahim escreveu:
#
Hi again
the data represents a  directed graph  represented in a matrix

e.g 
library(igraph)
g <- forest.fire.game(10, fw.prob=0.3 , bw.factor=0.32/0.3, directed = TRUE)

m=get.adjacency(g , attr=NULL)
print(m)

----------
many thanks and pardon me for multiple posts
Ragia

  
  
#
On Apr 6, 2015, at 11:15 AM, Bert Gunter wrote:

            
This should correct the problem:
+           apply(M, 1, paste0, collapse=".") ,
+           decreasing=TRUE), ]
      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
 [1,]    0    1    1    1    0    0    0    0    0     0
 [2,]    0    0    0    1    1    0    1    0    0     0
 [3,]    1    0    0    0    0    0    0    0    0     0
 [4,]    1    0    0    0    0    0    0    0    0     0
 [5,]    1    0    0    0    0    0    0    0    0     0
 [6,]    0    1    0    0    0    0    0    0    0     0
 [7,]    0    1    0    0    0    0    0    0    0     0
 [8,]    0    0    0    0    1    0    0    0    0     0
 [9,]    0    0    0    0    1    0    0    0    0     0
[10,]    0    0    0    0    0    0    0    0    0     0

The original matrix was sparse and I get this error message when attempting to use 'order' in the i-argument to the `[` method for dgCMatrix: 

Error in m[order(rowSums(m == "1"), apply(m, 1, paste0, collapse = "."),  : 
  error in evaluating the argument 'i' in selecting a method for function '[': Error: not-yet-implemented method for ==(<dgCMatrix>, <character>).
 ->>  Ask the package authors to implement the missing feature.


HTH;