Say I have this example graph, i want to find the edges connected to vertex 'a'
假设我有这个示例图,我想找到连接到顶点'a'的边
d <- data.frame(p1=c('a', 'a', 'a', 'b', 'b', 'b', 'c', 'c', 'd'),
p2=c('b', 'c', 'd', 'c', 'd', 'e', 'd', 'e', 'e'))
library(igraph)
g <- graph.data.frame(d, directed=FALSE)
print(g, e=TRUE, v=TRUE)
I can easily find a vertex:
我可以很容易地找到一个顶点:
V(g)[V(g)$name == 'a' ]
But i need to reference all the edges connected to the vertex 'a'.
但我需要引用连接到顶点'a'的所有边。
4 个解决方案
#1
25
See the documentation on igraph iterators; in particular the from() and to() functions.
请参阅igraph迭代器的文档;特别是from()和to()函数。
In your example, "a" is V(g)[0], so to find all edges connected to "a":
在您的示例中,“a”是V(g)[0],因此要查找连接到“a”的所有边:
E(g) [ from(0) ]
Result:
结果:
[0] b -- a
[1] c -- a
[2] d -- a
#2
4
If you do not know the index of the vertex, you can find it using match() before using the from() function.
如果您不知道顶点的索引,可以在使用from()函数之前使用match()找到它。
idx <- match("a", V(g)$name)
E(g) [ from(idx) ]
#3
3
Found a simpler version combining the two efforts above that may be useful too.
找到一个更简单的版本,结合上面的两个努力,也可能是有用的。
E(g)[from(V(g)["name"])]
#4
1
I use this function for getting number of edges for all nodes:
我使用此函数获取所有节点的边数:
sapply(V(g)$name, function(x) length(E(g)[from(V(g)[x])]))
#1
25
See the documentation on igraph iterators; in particular the from() and to() functions.
请参阅igraph迭代器的文档;特别是from()和to()函数。
In your example, "a" is V(g)[0], so to find all edges connected to "a":
在您的示例中,“a”是V(g)[0],因此要查找连接到“a”的所有边:
E(g) [ from(0) ]
Result:
结果:
[0] b -- a
[1] c -- a
[2] d -- a
#2
4
If you do not know the index of the vertex, you can find it using match() before using the from() function.
如果您不知道顶点的索引,可以在使用from()函数之前使用match()找到它。
idx <- match("a", V(g)$name)
E(g) [ from(idx) ]
#3
3
Found a simpler version combining the two efforts above that may be useful too.
找到一个更简单的版本,结合上面的两个努力,也可能是有用的。
E(g)[from(V(g)["name"])]
#4
1
I use this function for getting number of edges for all nodes:
我使用此函数获取所有节点的边数:
sapply(V(g)$name, function(x) length(E(g)[from(V(g)[x])]))