igraph in R : how can I build conditional edges?

128 views Asked by At

Let us say, I have a set of conditions for the edges. For instance, draw an edge if and only if a column value is greater than 1 ? how to do that in igraph?

(g1 <- graph( edges=c("C","A","A","B","B","A","C","B"), directed=T )

can the edges = to a function that dictates the condition and if true, they should draw an edge?

1

There are 1 answers

0
clp On

As an example consider:

## Keep edges iff vertex <from> is smaller then vertex <to>
set.seed(1)
g1  <- sample_gnm(20, 12, directed = TRUE)      # reproducible 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) # make graph from filter.

Source graph:

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

Result:

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