0
votes

I have a problem with one-row matrix, I cannot load it to a directed graph

> network_matrix
     login      mentions weight
[1,] "rtomayko" "author" "1"   
> str(network_matrix)
 chr [1, 1:3] "rtomayko" "author" "1"
 - attr(*, "dimnames")=List of 2
  ..$ : NULL
  ..$ : chr [1:3] "login" "mentions" "weight"
> typeof(network_matrix)
[1] "character"
> g = graph.edgelist(network_matrix[,1:2], directed = TRUE)
Error in graph.edgelist(network_matrix[, 1:2], directed = TRUE) : 
  graph_from_edgelist expects a matrix with two columns
> 

3 rd column is weight and I'd like to skip it

> is.matrix(network_matrix)
[1] TRUE
> ncol(network_matrix)
[1] 3

Which is fine, but:

> ncol(network_matrix[,1:2])
NULL

Which I don't understand why is NULL and prevents igraph structure.generators.R code from running because of the ncol(el) != 2 check.

This is my code which worked on previous data and which I'd like to run now:

g = graph.edgelist(network_matrix[,1:2], directed = TRUE)
E(g)$weight=as.numeric(network_matrix[,3])

Thanks!

1
g = graph.edgelist(network_matrix[,1:2, drop=F], directed = TRUE) - user20650
Hi. Great, it helped. I'll accept it as soon as it's written as an SO answer, where it'll be explained what does drop do. - oski86
oski, this will explain the drop argument .. also have a read of ?'[' - user20650

1 Answers

1
votes

It was necessary to set the drop=F

g = graph.edgelist(network_matrix[,1:2, drop=F], directed = TRUE)

thanks @user20650

case closed