【问题标题】:Query vertex attributes values dynamically without adjacency matrix in R在R中没有邻接矩阵的情况下动态查询顶点属性值
【发布时间】:2019-11-05 03:37:46
【问题描述】:

我是 igraph 的新手,在文档方面有点挣扎。我正在尝试动态查询顶点属性的值(无需对属性名称进行硬编码)。我需要在不使用邻接矩阵的情况下,使用属性名称(而不是索引)一一获取每个属性。

require(igraph)
graph <- make_ring(9)  #this is my original graph
V(graph)$name <- c("A", "B", "C", "D", "E", "F", "G", "H", "I")  #name of vertices
V(graph)$att1 <- c(1,0,0,0,1,0,0,1,0)
V(graph)$att2 <- c(0,3,1,0,0,1,0,0,1)
V(graph)$att3 <- c(0,0,0,1,4,0,0,0,0)
V(graph)$att4 <- c(1,0,1,0,2,1,0,0,0)
node1 <- V(graph)[1]
node1$att1 # how do I do this without having to hardcode the name of the attribute?

假设我正在使用 for 循环来查询 node1 的选定属性的值:

selected_att <- c("att1", "att3")
for (i in selected_att)
{
   node1$i  #--> how do I get the value of attribute i for node1?
}

为了提供更多上下文,我正在尝试使用内部和相互耦合的属性相似性来计算图中每个节点之间的节点相似性,以计算两个节点之间的整体相似性。使用邻接矩阵是查询每对节点的属性值以计算其整体属性耦合相似度的最有效方法吗?我的图相当大,所以我试图避免构建邻接矩阵。结果将是一个相似度矩阵(在每对节点之间)。

【问题讨论】:

    标签: r igraph


    【解决方案1】:

    您不能将$ 与右侧的变量一起使用。您可以使用更通用的vertex_attr 函数。例如你可以做

    selected_att <- c("att1", "att3")
    for (i in selected_att)
    {
       print(vertex_attr(graph, i, node1))
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多