开发者

Reading edges from a file. I can't define a graph

I am new in R. I am working with igraph library. I am new using such library.

I have a problem:

I have a list of edges in a text file. It has two columns. The first has initial node, the second has the ending node.

I am reading the file with:

g1 <-read.table ("g1.txt")

The reading is successfull.

with ls.str(g1) i get:

V1 :  int [1:995] 0 0 0 0 0 0 0 0 0 0 ...
V2 :  int [1:995] 2 3 4 5 6 7 8 9 10 11 ...

when i try to defin开发者_JAVA百科e the graph with the just loaded edges I get:

Error in graph(g1) : (list) object cannot be coerced to type 'double'

How i could to define the graph from file's edges avoiding the above error?


As @Sacha Epskamp suggested, as.matrix may sort this out, possibly with a transpose.

The following recreates your error message and then produces a graph from the same data

> library(igraph)
> g1 <- data.frame( V1 = c(0,0,0,0), V2 = c(2,3,4,5) )
> g1
  V1 V2
1  0  2
2  0  3
3  0  4
4  0  5
>
> graph(g1)
Error in graph(g1) : (list) object cannot be coerced to type 'double'
> 
> g2 <- t(as.matrix(g1))
> g2
   [,1] [,2] [,3] [,4]
V1    0    0    0    0
V2    2    3    4    5
>
> graph(g2)
Vertices: 6 
Edges: 4 
Directed: TRUE 
Edges:

[0] 0 -> 2
[1] 0 -> 3
[2] 0 -> 4
[3] 0 -> 5
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜