【问题标题】:R - Make new graph object using vertex attribute igraphR - 使用顶点属性 igraph 创建新的图形对象
【发布时间】:2017-04-15 16:52:02
【问题描述】:

我有一个有向图对象,其中两列节点(id、类型)和两列边(从、到)。有没有一种简单的方法来获取将 type 设置为顶点的图形?

顶点:

id type
1  a
2  a
3  b
4  c

边:

from to
1    2
2    4
3    4
1    3

转换为:

顶点:

id type
a  a
b  b
c  c

边:

from to
a    a
a    c
b    c
a    b

我用这段代码制作图表来获取上面的第一个图表对象:

nodes <- read.csv("1.toyNODES.CSV", header=T, as.is=T)
edges <- read.csv("1.toyEDGES.CSV", header=T, as.is=T)
graph <- graph_from_data_frame(d=edges, vertices=nodes, directed=T)

【问题讨论】:

    标签: r graph adjacency-matrix


    【解决方案1】:

    这将聚合组之间的边:

    type.factor <- as.factor(V(your.g)$type)
    type <- as.numeric(type.factor)
    meta.g <- contract.vertices(your.g,type)
    E(meta.g)$weight <- 1
    meta.g <- simplify(meta.g)
    

    科拉奇克/恰尔迪

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-20
      • 1970-01-01
      • 2019-05-10
      • 2018-11-25
      • 1970-01-01
      • 2021-09-04
      相关资源
      最近更新 更多