【问题标题】:Igraph: export list vertices with vertex and edge attributesIgraph:导出具有顶点和边属性的列表顶点
【发布时间】:2016-05-28 13:38:10
【问题描述】:

我有美国城市之间的贸易数据。我有城市(节点)和贸易(边缘)的属性数据。

考虑下图:

library(igraph)
gg <- graph.atlas(711)
V(gg)$name <- 1:7
V(gg)$city <- c("BOISE","NEW YORK","NEW YORK","BOISE","BOISE","LA","LA")
V(gg)$color <- ifelse(V(gg)$city=="BOISE", "orange","yellow")
gg <- delete.edges(gg, E(gg,P=c(1,2,2,3,2,7,7,6,7,3,3,4,3,5,4,5,5,6,6,1))) 
gg <- add.edges(gg,c(1,4,4,5,5,1),attr=list(trade=1))
gg <- add.edges(gg,c(7,5,5,4,4,7),attr=list(trade=2))
gg <- add.edges(gg,c(7,3,3,5,5,7),attr=list(trade=3))
gg <- add.edges(gg,c(2,7,7,6,6,2),attr=list(trade=4))
gg <- add.edges(gg,c(6,4),attr=list(trade=5))
plot(gg, edge.label=E(gg)$trade)

生成以下内容:

由此,我想将包含边缘属性的边缘列表导出到文本文件。

例如:

[CITY 1], [CITY 2], [TRADE]

对我如何做到这一点有任何帮助吗?这似乎很容易,但我真的被困住了。

【问题讨论】:

    标签: r attributes igraph


    【解决方案1】:

    您可以提取边缘列表,然后使用它来获取顶点属性并将其与边缘属性结合。例如

    el <- as_edgelist(gg)
    data.frame(city1=V(gg)[el[,1]]$city, 
        city2=V(gg)[el[,2]]$city, 
        trade=E(gg)$trade)
    
    #       city1 city2 trade
    # 1     BOISE BOISE     1
    # 2     BOISE BOISE     1
    # 3     BOISE BOISE     1
    # 4     BOISE    LA     2
    # 5     BOISE BOISE     2
    # 6     BOISE    LA     2
    # 7  NEW YORK    LA     3
    # 8  NEW YORK BOISE     3
    # 9     BOISE    LA     3
    # 10 NEW YORK    LA     4
    # 11       LA    LA     4
    # 12 NEW YORK    LA     4
    # 13    BOISE    LA     5
    

    数据看起来有点奇怪,因为您有多个具有相同城市名称的顶点。不确定这是否是您的意图。

    【讨论】:

    • 完美!再次感谢 MrFlick!
    最近更新 更多