【问题标题】:igraph in R : how can I build conditional edges?R中的igraph:如何构建条件边?
【发布时间】:2022-11-07 04:55:37
【问题描述】:

让我们说,我有一组边缘条件。例如,当且仅当列值大于 1 时才绘制边?如何在 igraph 中做到这一点?

(g1 <- 图(边=c("C","A","A","B","B","A","C","B"), 定向=T)

边缘 = 可以指定条件的函数,如果为真,它们应该画一条边吗?

【问题讨论】:

  • 答案是:是的。但更具体地说,应该描述功能的域和图像。这将有助于添加一个小的具体示例。

标签: r igraph uigraphicscontext


【解决方案1】:

例如,考虑:

## Keep edges iff vertex <from> is smaller then vertex <to>
set.seed(1)
g1  <- sample_gnm(20, 12, directed = TRUE)      # repreducible random graph
V(g)$name <- as.character(V(g1))                # identify vertices by name
el <- as_edgelist(g1)                           # output graph to edgelist

cc <- as.numeric(el[,1]) < as.numeric(el[,2])   # calculate condition 
fel <- el[cc,]                                  # and filter

g2 <- graph_from_edgelist(fel, directed = TRUE) # restore graph from fiterd edgelist

源图:

IGRAPH 0623cb0 D--- 20 12 -- Erdos-Renyi (gnm) graph
+ attr: name (g/c), type (g/c), loops (g/l), m (g/n)
+ edges from 0623cb0:
 [1]  3-> 2  4->12  5-> 9  5->11  8->20  8->10  8->12  9->12 10->16 16->19
[11] 19-> 4 19->11

结果:

IGRAPH 063424f D--- 20 9 -- 
+ edges from 063424f:
[1]  4->12  5-> 9  5->11  8->20  8->10  8->12  9->12 10->16 16->19

【讨论】:

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