我正在使用 igraph R中的包。
我想将一些数据与每个顶点相关联,例如通过向每个添加 id 和 description 属性。属性是在运行时确定的。我有几个关于如何设置和获取这些数据的相关问题。
要设置我使用的顶点 id(其中 g
是一个图形):
> set.vertex.attribute(g,'id',1,'first_id') # etc
我希望能够通过以下方式读取属性:
> get.vertex.attribute(g,'id',1)
但这会返回 NULL。难道我做错了什么?
进一步,该函数带有
get.vertex.attribute
call 无权访问属性名称列表。如何从图中提取属性名称 g
?最后,我想从/到命名列表中设置/获取属性的值。有没有一种简单的方法来做到这一点,而无需遍历每个顶点和属性并应用
set.
- 或 get.vertex.attribute
?谢谢!
最佳答案
看起来你必须分配 set.vertex.attribute
的结果返回 g
像这样:
g <- graph.data.frame(data.frame(one=1:2,two=2:3))
g <- set.vertex.attribute(g,'id',1,'first_id')
get.vertex.attribute(g,'id',1)
#[1] "first_id"
作为帮助在
?get.vertex.attribute
说:graph: The graph object to work on. Note that the original graph is never modified, a new graph object is returned instead; if you don't assign it to a variable your modifications will be lost! See examples below.
此外,从同一个帮助文件中有...
list.graph.attributes, list.vertex.attributes and list.edge.attributes return a character vector, the names of the attributes present.
list.vertex.attributes(g)
#[1] "name" "id"
乍一看,似乎没有一个简单的函数来写入/写入顶点属性。你可以编造这样的东西:
lapply(list.vertex.attributes(g),function(x) get.vertex.attribute(g,x))
#[[1]]
#[1] "1" "2" "3"
#
#[[2]]
#[1] "first_id" NA NA
关于r - 如何列出R中的所有图形顶点属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19993485/