oski86
oski86

Reputation: 865

Cannot get igraph edgelist working, ncol for matrix returns NULL

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!

Upvotes: 0

Views: 643

Answers (1)

oski86
oski86

Reputation: 865

It was necessary to set the drop=F

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

thanks @user20650

case closed

Upvotes: 1

Related Questions