如何从图中返回顶点ID

时间:2021-11-27 15:14:59

Please consider the following

请考虑以下事项

library(igraph)
id <- c("1","2","A","B")
name <- c("02 653245","03 4542342","Peter","Mary")
category <- c("digit","digit","char","char")
from <- c("1","1","2","A","A","B")
to <- c("2","A","A","B","1","2")

nodes <- cbind(id,name,category)
edges <- cbind(from,to)

g <- graph.data.frame(edges, directed=TRUE, vertices=nodes)

Now I want to access a specific vertex of the graph using the ids I used to create the graph from the data frame id <- c("1","2","A","B").

现在我想使用我用于从数据帧id < - c(“1”,“2”,“A”,“B”)创建图形的ID访问图形的特定顶点。

Let's say I want to access the third vertex - originally identified with "A". Is there any way to access the vertex with something like

假设我想访问第三个顶点 - 最初用“A”标识。有没有办法用类似的东西访问顶点

V(g)$id == "A"

And is there anyway to obtain the id from name? That is, if I run

无论如何从名字中获取id?也就是说,如果我跑

which(V(g)$name == "Peter")

I get 3. How to get A instead?

我得到3.如何获得A代替?

2 个解决方案

#1


22  

First of all, igraph uses the vertex attribute name as symbolic ids of vertices. I suggest you add the ids as name and use another name for the other attributes, e.g. realname.

首先,igraph使用顶点属性名称作为顶点的符号id。我建议您添加ID作为名称,并使用其他名称作为其他属性,例如真正的名字。

But often you don't need to know the numeric ids if you are using symbolic names, because all functions accepts (well, they should) symbolic ids as well. E.g. if you want the degree of vertex Peter, you can just say degree(g, "Peter").

但是,如果使用符号名称,通常不需要知道数字ID,因为所有函数也接受(嗯,它们应该)符号id。例如。如果你想要顶点彼得的程度,你可以说度(g,“彼得”)。

If you really want the the numeric id, you can do things like these:

如果你真的想要数字id,你可以做以下事情:

as.numeric(V(g)["Peter"])
# [1] 3
match("Peter", V(g)$name)
# [1] 3

If you want to get to id from name in your example, you can just index that vector with the result:

如果你想在你的例子中从name获取id,你可以用结果索引该向量:

id[ match("Peter", V(g)$name) ]

#2


0  

The answer might be useful. My recommendation is same as of @Gabor Csardi about id as name, and name as real_name.

答案可能有用。我的建议与@Gabor Csardi的建议相同,id为name,名称为real_name。

library(igraph)
name <- c("1","2","A","B")
real_name <- c("02 653245","03 4542342","Peter","Mary")
category <- c("digit","digit","char","char")
from <- c("1","1","2","A","A","B")
to <- c("2","A","A","B","1","2")

nodes <- cbind(name,real_name,category)
edges <- cbind(from,to)

g <- graph.data.frame(edges, directed=TRUE, vertices=nodes)

list.vertex.attributes(g)
#Output: [1] "name"      "real_name" "category"
which(V(g)$real_name == "Peter")
#Output: [1] 3
V(g)$name[which(V(g)$real_name == "Peter")]
#Output: [1] "A"

#1


22  

First of all, igraph uses the vertex attribute name as symbolic ids of vertices. I suggest you add the ids as name and use another name for the other attributes, e.g. realname.

首先,igraph使用顶点属性名称作为顶点的符号id。我建议您添加ID作为名称,并使用其他名称作为其他属性,例如真正的名字。

But often you don't need to know the numeric ids if you are using symbolic names, because all functions accepts (well, they should) symbolic ids as well. E.g. if you want the degree of vertex Peter, you can just say degree(g, "Peter").

但是,如果使用符号名称,通常不需要知道数字ID,因为所有函数也接受(嗯,它们应该)符号id。例如。如果你想要顶点彼得的程度,你可以说度(g,“彼得”)。

If you really want the the numeric id, you can do things like these:

如果你真的想要数字id,你可以做以下事情:

as.numeric(V(g)["Peter"])
# [1] 3
match("Peter", V(g)$name)
# [1] 3

If you want to get to id from name in your example, you can just index that vector with the result:

如果你想在你的例子中从name获取id,你可以用结果索引该向量:

id[ match("Peter", V(g)$name) ]

#2


0  

The answer might be useful. My recommendation is same as of @Gabor Csardi about id as name, and name as real_name.

答案可能有用。我的建议与@Gabor Csardi的建议相同,id为name,名称为real_name。

library(igraph)
name <- c("1","2","A","B")
real_name <- c("02 653245","03 4542342","Peter","Mary")
category <- c("digit","digit","char","char")
from <- c("1","1","2","A","A","B")
to <- c("2","A","A","B","1","2")

nodes <- cbind(name,real_name,category)
edges <- cbind(from,to)

g <- graph.data.frame(edges, directed=TRUE, vertices=nodes)

list.vertex.attributes(g)
#Output: [1] "name"      "real_name" "category"
which(V(g)$real_name == "Peter")
#Output: [1] 3
V(g)$name[which(V(g)$real_name == "Peter")]
#Output: [1] "A"