0

I want to create a matrix (11 row and 2 columns) based on igraph formulas. It's not working

bin_node_size<-vcount(g) #network size (number of nodes)
wei_node_str<-graph.strength(g) #node strenght of each vertex
bin_node_deg<-degree(g) #node degree for each vertex
node_matrix<-as.matrix(c(wei_node_str, bin_node_deg), row=bin_node_size, col=2)

Here are the outputs of the formulas above

bin_nodesize
[1] 11

wei_node_str
 A  B  C  G  D  E  F  K  H  I  J 
19  3  5  5  5  9  3  4  1  3  3 

bin_node_deg
 A B C G D E F K H I J 
 6 2 2 2 1 3 1 2 1 1 1 

1) I have 11 nodes , but I don't want to write "11" under row=11, I want to use the node degree to determiner the number of rows in my matrix.

2) in the event I replace row=row=bin_node_size for row=11, I end up with a matrix of 1 column instead of 2 ? even if the command as.matrix specifies to get 2 columns.

user123456
  • 67
  • 1
  • 1
  • 10
  • 1
    It would be easier to help you with a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with clear input and the desired output. But `as.matrix` doesn't have parameters named `row=` and `col=`, it has parameters named `nrow=` and `ncol=`. – MrFlick Sep 01 '17 at 18:40

1 Answers1

0

Try using rbind instead of c() and ncol/nrow instead of col/row:

node_matrix<-as.matrix(rbind(wei_node_str, bin_node_deg), 
                   nrow=11, ncol=2)

Output is a matrix as specified:

      A B C G D E F K H I J
[1,] 19 3 5 5 5 9 3 4 1 3 3
[2,]  6 2 2 2 1 3 1 2 1 1 1

Sample data for example:

require(data.table)

wei_node_str <- fread("
A  B  C  G  D  E  F  K  H  I  J 
19  3  5  5  5  9  3  4  1  3  3")

bin_node_deg <- fread("
A B C G D E F K H I J 
6 2 2 2 1 3 1 2 1 1 1 ")
www
  • 4,124
  • 1
  • 11
  • 22