r - 如何列出R中的所有图形顶点属性?

标签 r igraph

我正在使用 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/

相关文章:

r - 使用 Brew、Pander 和 R 自动化 LaTeX 报告

r - 条件概率

igraph;节点间距离

r - 循环图中的部分结果

r - 创建一个PDF表格

r - 在 Rmarkdown HTML 输出中抑制来自 RStan 的正在运行的消息

r - 为什么我没有使用 ggplot2 获得两个图例?

r - 如何在igraph中将边缘标签与边缘分开?

python - 如何使用 R (igraph) 或 python 对不同类型的不同节点集进行建模?

c++ - "excess elements in scalar initializer"使用 igraph C 库生成具有幂律度分布的网络时