问题与R“igraph”封装邻居功能 [英] issues with R "igraph" package neighbor function

查看:327
本文介绍了问题与R“igraph”封装邻居功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近开始使用Rigraph软件包(版本0.7)。我写了一个简单的程序来理解包的基本知识(将数据读入图对象,获得节点的邻居)。我正在使用其顶点从0开始的图形。图中的边缘会根据需要填充,但是当我尝试获取节点的邻接列表/邻居时,我发现它并没有给出我期望的结果。有人可以帮我解决这个问题吗?或者指出如果我缺少一些微不足道的东西?



下面是我写的图表数据/代码:



图形边缘数据(第一列包含第一个顶点,第二列包含第一个顶点有边的顶点)

  0 5 
1 5
2 5
3 5
0 2

>库('igraph')
> graph_file_ori = read.table(test.txt)
> graph_obj = graph.data.frame(graph_file_ori,directed = FALSE)
> str(graph_obj)
IGRAPH UN - 5 5 -
+ attr:name(v / c)
+ edges(顶点名称):

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

>邻居(graph_obj,0)
.Call中的错误(R_igraph_neighbors,graph,as.igraph.vs(graph,v) - 1,:
在type_indexededgelist.c:750处:无法获取邻居,无效的顶点ID
>邻居(graph_obj,1)
[1] 3 5
>邻居(graph_obj,2)
[1] 5
>邻居(graph_obj,3)
[1] 1 5
>邻居(graph_obj,4)
[1] 5
>邻居(graph_obj,5)
[1] 1 2 3 4

根据图表,预期产出为:

  neighbours(0)= 5,2 
neighbors(1)= 5
neighbors(2)= 5, 0
neighbours(3)= 5

4的等级为0.
欣赏帮助。

解决方案

如果您想使用符号顶点名称,那很好,但您需要使用它们你的查询也是如此。即写
$ b $ pre $ $ c $ neighbour(graph_obj,0)

如果您想要结果作为符号名称,那么你需要做的是

pre $ $ $ $ $ $ $ $ $ $ $ $ $ $ c>

从igraph版本0.8开始这将不再需要。


I started using the R "igraph" package recently(version 0.7). I wrote a simple program to understand the basics of the package(reading data into the graph object, getting the neighbours of a node). I am using a graph whose vertices start at 0. The edges in the graph are populated as I need it, however when I attempt to get the adjacency list/ neighbours of a node, I observed it was not giving the result that I expected. Can someone help me out with this/ or point out if I am missing something trivial ?

Below is the graph data/code that I wrote:

Graph Edge Data(first column contains the first vertex, second column contain the vertex to which there is an edge from the first vertex)

0 5
1 5
2 5
3 5
0 2

> library('igraph')
> graph_file_ori = read.table("test.txt")
> graph_obj = graph.data.frame(graph_file_ori,directed=FALSE)
> str(graph_obj)
IGRAPH UN-- 5 5 -- 
+ attr: name (v/c)
+ edges (vertex names):

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

> neighbors(graph_obj,0)
Error in .Call("R_igraph_neighbors", graph, as.igraph.vs(graph, v) - 1,  : 
At type_indexededgelist.c:750 : cannot get neighbors, Invalid vertex id
> neighbors(graph_obj,1)
[1] 3 5
> neighbors(graph_obj,2)
[1] 5
> neighbors(graph_obj,3)
[1] 1 5
> neighbors(graph_obj,4)
[1] 5
> neighbors(graph_obj,5)
[1] 1 2 3 4

Based on the graph, the expected output was :

neighbors(0) = 5,2
neighbors(1) = 5
neighbors(2) = 5,0
neighbors(3) = 5

Degree of 4 is 0. Appreciate the help.

解决方案

If you want to use symbolic vertex names, that is fine, but then you need to use them in your queries as well. I.e. write

neighbors(graph_obj, "0")

If you want the results as symbolic names as well, then you need to do

graph_obj$name[neighbors(graph_obj, "0")]

This will be not needed from igraph version 0.8.

这篇关于问题与R“igraph”封装邻居功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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