R iGraph:如何选择满足特定规则的顶点

这应该是一个很容易的问题,但我真的不知道如何做到这一点.我有,比如说,图中的图表:

《R iGraph:如何选择满足特定规则的顶点》

每个节点都有一个日期,我想找到具有最大出度的节点,只有那些在中位数之前有日期的节点.
我试过这个:

library(igraph)
nodes <- data.frame(name=c("a", "c", "d", "e", "f", "g", "i", "j", "k"),
                    date = c(27,   13,  0,   18,  0,   8,   44,  26, 22))
relations <- data.frame(from=c("d", "d", "f", "f","f", "g","g","g","c","c", "e"),
                        to=c("i", "f","d","c","g","k","a","c","a", "e","j"))
ggg <- graph.data.frame(relations, directed=TRUE, vertices=nodes)

V(ggg)$label <- V(ggg)$name
plot(ggg, layout = layout.fruchterman.reingold.grid, edge.curved=FALSE, 
           edge.arrow.size=0.2,edge.arrow.width=0.4)

V(ggg)$label <- V(ggg)$date
plot(ggg, layout = layout.fruchterman.reingold.grid, edge.curved=FALSE, 
     edge.arrow.size=0.2,edge.arrow.width=0.4)

median_delay <- median(V(ggg)$date)
vert_before_median <- V(ggg)[  V(ggg)$date <= median_delay  ]
wnodes <- V(ggg)$name[ degree(ggg,v=vert_before_median,mode="out")==max(degree(ggg,v=vert_before_median,mode="out")) ] 

一切似乎都可以到这里:

> degree(ggg,v=vert_before_median,mode="out")==max(degree(ggg,v=vert_before_median,mode="out"))
    c     d     e     f     g 
FALSE FALSE FALSE  TRUE  TRUE 

但是,当我想要保存满足此属性的节点时,我遇到了麻烦.我认为wnodes应该包含节点“f”和“g”

> wnodes
[1] "e" "f" "k"

似乎我在尝试从图中选择顶点的方式中遗漏了一些东西.我试过了,但它仍然是不对的:

> V(ggg)[which( degree(ggg,v=vert_before_median,mode="out")==max(degree(ggg,v=vert_before_median,mode="out"))   )]
Vertex sequence:
[1] "e" "f"

任何的想法?

最佳答案 子集vert_before_median而不是所有顶点,你应该得到你所追求的,

wnodes <- vert_before_median[ degree(ggg,v=vert_before_median,mode="out")==max(degree(ggg,v=vert_before_median,mode="out")) ]

在上面的代码中,当您使用长度为9的所有顶点时,R会自动循环遍历长度为5的逻辑向量(FALSE,FALSE,FALSE,TRUE,TRUE).

点赞