Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R iGraph: How to select vertices satisfying a certain rule

Tags:

r

igraph

It should be an easy enough question, but I don't really know how to do this. I have, say, the graph in the figure:

enter image description here

Every node has a date and I want to find the nodes with largest out-degree, only among the ones that have a date before the median. I have 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 OK up to here:

> 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 hold the nodes that satisfy this property, I get into trouble. I thought wnodes should contain nodes "f" and "g", instead

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

It seems I'm missing something in the way I try to select vertices from the graph. I have tried with which, but still it's not right:

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

like image 387
Nonancourt Avatar asked Jun 17 '16 11:06

Nonancourt


People also ask

What is a vertex in igraph?

Vertex sequences are usually used as igraph function arguments that refer to vertices of a graph. A vertex sequence is tied to the graph it refers to: it really denoted the specific vertices of that graph, and cannot be used together with another graph.

How do you delete edges in igraph?

The minus operator (' - ') can be used to remove vertices or edges from the graph. The operation performed is selected based on the type of the right hand side argument: If it is an igraph graph object, then the difference of the two graphs is calculated, see difference .

What is a subgraph in R?

subgraph creates a subgraph of a graph, containing only the specified vertices and all the edges among them.


1 Answers

Subset vert_before_median instead of all vertices and you should get what you're after,

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

In your code above R automatically cycles through the logical vector of length 5 (FALSE, FALSE, FALSE, TRUE, TRUE) when you use it to subset all vertices which has a length of 9.

like image 59
Ryan Haunfelder Avatar answered Oct 25 '22 21:10

Ryan Haunfelder