【问题标题】:Discard Bipartite Edges on igraph in R在R中的igraph上丢弃二分边
【发布时间】:2018-02-18 13:14:59
【问题描述】:

我希望丢弃 iGraph 上的二分边,以便我只看到那些拥有超过 3 个成员资格的人。我正在查看参加过classes 的students 的数据集。如果每个班级有超过 3 名学生,我只想查看连接所基于的班级图表。

students | class
Jane     | Biology
Jack     | Biology
Mark     | Biology
Steve    | Biology
Jane     | Chemistry
Jack     | Chemistry
Mark     | Chemistry
Steve    | Chemistry
Lane     | Physics
Steve    | Physiology

我希望最后得到一个只有 Biology 和 Chemistry 的图表,并在它们之间画一条线,因为这两个班级都有学生会员 > 3

这是我目前所拥有的:

g.students
cl<- clusters(g.students)
g.students<- delete.vertices(g.students, names(cl$membership[ cl$membership > 3]))

不知何故,当我输入这个时,我得到了这个,这是不可能的,因为应该有节点和边:

IGRAPH c2cf5c3 UN-B 1 0 -- 

+ attr: name (v/c), type (v/l)

我不知道为什么会这样。有没有其他方法可以丢弃这些边缘?

【问题讨论】:

  • 所以根本没有物理学和生理学?然后听起来,总的来说,最终结果将是一个完整的图,其中顶点是> 3个学生的班级。对吗?
  • 继续@Julius 的问题,您是希望所有节点都连接,还是只希望两个班级共享学生?
  • 是的,这是正确的,是的,我只想表示两个班级共享超过 3 个学生的边缘。

标签: r igraph bipartite


【解决方案1】:

这是一种方法:

A <- as_adj(g.students) # Extracting the adjacency matrix
B <- as.matrix(A[unique(df$class), unique(df$students)]) # Selecting its relevant part
C <- B %*% t(B) # Numbers of common students between classes
C <- ifelse(C > 3, 1, 0) * (1 - diag(nrow(C))) # Setting diagonal to zero, discarding 
                                               # edges, and setting remaining weights to 1
C <- C[colSums(C) > 0, colSums(C) > 0]         # Discarding vertices
g <- graph_from_adjacency_matrix(C)            # Obtaining a graph

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多