1

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 define 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?

4

1 回答 1

5

正如@Sacha Epskamp 所建议的as.matrix那样,可以通过转置来解决这个问题。

以下重新创建您的错误消息,然后从相同的数据生成图表

> 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
于 2011-07-03T23:45:21.417 回答