【问题标题】:Extracting an edge list with conditions from an igraph object in R从R中的igraph对象中提取带有条件的边列表
【发布时间】:2019-02-28 16:05:54
【问题描述】:

我正在处理一个由不同类型的节点组成的无向 igraph 对象(例如,黄色的男性 M 和橙色的女性 F):

g <- graph.atlas(711)
V(g)$name <- 1:7
V(g)$gender <- c("M","F","M","M","M","F","F")
V(g)$color <- ifelse(V(g)$gender=="F", "orange","yellow")
g<-delete.edges(g, E(g, P=c(1,2,2,3,2,7,7,6,7,3,3,4,3,5,4,5,5,6,6,1))) 
g<-add.edges(g,c(1,4,4,5,5,1,4,7,7,3,3,5,5,7,2,7,7,6,6,2,6,4))
plot(g)

我想提取一个由连接不同类型(男性和女性)节点的边组成的边列表:

edgelist <- rbind(c(3,7),
               c(4,6),
               c(4,7),
               c(5,7))

assortativity 使用连接 M 和 F 类型顶点的边的分数,但我不知道要明确提取这些边。

get.edgelist只返回整个边列表,不能设置条件。

【问题讨论】:

    标签: r attributes igraph edge-detection


    【解决方案1】:

    您可以使用%--% 选择器来查找连接男性节点和女性节点的边。例如

    E(g)[V(g)[gender=="M"] %--% V(g)[gender=="F"]]
    

    V(g)[gender=="M"] 查找所有“男性”节点,V(g)[gender=="F"] 查找所有女性节点,%--% 查找两个集合之间的所有边。

    【讨论】:

    • 感谢分享!我不知道这个 igraph 选择器,它看起来像 it has some variants for directed networks
    • 谢谢!然后我可以使用 ends(g, E(g)[V(g)[gender=="M"] %--% V(g)[gender=="F"]]) 来获得矩阵中的边缘列表:)
    【解决方案2】:
    edges = get.edgelist(g)
    edges[rowSums(apply(edges, 2, function(x) get.vertex.attribute(g, "gender", x)) == "M") == 1,]
    #     [,1] [,2]
    #[1,]    4    7
    #[2,]    3    7
    #[3,]    5    7
    #[4,]    4    6
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-15
      • 2017-01-07
      • 2021-06-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-07
      • 1970-01-01
      相关资源
      最近更新 更多