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?

Guillermo Parada
  • 199
  • 1
  • 1
  • 6
  • It would help us answer your question if you could provide a small sample of the text file. If the file is small, you can try using `dput`. `ls.str` has as its first argument http://stat.ethz.ch/R-manual/R-patched/library/utils/html/ls_str.html: pos = -1. I suspect it's trying to coerce your list into a double to enter into the pos argument. Unlikely to be what you want! – Ari B. Friedman Jul 03 '11 at 22:26
  • 2
    Maybe `as.matrix()` on the object works? – Sacha Epskamp Jul 03 '11 at 23:23

1 Answers1

5

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
Henry
  • 6,704
  • 2
  • 23
  • 39