从文件读取边缘.我无法定义图 [英] Reading edges from a file. I can't define a graph

查看:91
本文介绍了从文件读取边缘.我无法定义图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是R语言的新手.我正在使用igraph库.我是新来的使用这种库的人.

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

我有问题:

我在文本文件中有一个边列表.它有两列.第一个具有初始节点,第二个具有终止节点.

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")

阅读成功.

ls.str(g1)我得到:

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?

推荐答案

正如@Sacha Epskamp所建议的那样,as.matrix可能会对此进行整理,并且可能会进行转置.

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

这篇关于从文件读取边缘.我无法定义图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆