R iGraph: how to select vertices that satisfy a specific rule

This should be a pretty simple question, but I really don't know how to do this. I have, say, the graph in the figure:

enter image description here

Each node has a date, and I want to find the nodes with the greatest degree, only among those that have a date before the median. I tried this:

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")) ] 

Everything seems to be in order:

> 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 

But then, when I want to keep the nodes that satisfy this property, I have problems. I thought I wnodesshould contain the nodes "f" and "g", instead

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

It seems that I missed something while trying to select the vertices from the graph. I tried with which, but still it is not:

> 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"

Any idea?

+4
source share
1

vert_before_median , , ,

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

R 5 (FALSE, FALSE, FALSE, TRUE, TRUE), 9.

+2

All Articles