1

I am pretty new to R. I have an igraph graph object and I labeled the vertices TRUE or FALSE randomly. Is there a way to delete edges which are between two TRUE-labeled vertices or two FALSE-labeled vertices?

Here's my code :

g <- read_graph("grph.graphml", format = c("graphml"))
num_of_edges <- gorder(g)
random_list <- sample(c(TRUE,FALSE), num_of_vertices, TRUE) 
V(g)$label <- random_list
Winston
  • 601
  • 1
  • 9
  • 29
  • When asking for help, you should include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. Make sure all terms like `num_of_vertices` and `g` are defined. – MrFlick Mar 02 '18 at 21:33

1 Answers1

2

Your code refers to the file "grph.graphml" which we do not have, so I cannot use your example. Instead, I will use a random graph to illustrate.

## Generate random graph
library(igraph)
set.seed(1234)
g = erdos.renyi.game(15, 0.2)

## Your code to generate labels
random_list <- sample(c(TRUE,FALSE), gorder(g), TRUE) 
V(g)$label <- random_list

You can write a small function to test if the ends of an edge have the same labels and apply that to all edges. Then delete the ones where both ends are the same.

SameLabel = function(e) {
    V(g)[ends(g, e)[1]]$label == V(g)[ends(g, e)[2]]$label }
g2 = delete_edges(g, which(sapply(E(g), SameLabel)))

You can check that it is doing the right thing by plotting.

set.seed(1066)
LO = layout_with_fr(g)
par(mfrow=c(1,2), mar=c(1,1,1,1))
plot(g, layout=LO, frame=TRUE)
plot(g2, layout=LO, frame=TRUE)

reduced graph

Some of these look wrong because links between distant nodes go behind other nodes of the opposite type.

G5W
  • 36,531
  • 10
  • 47
  • 80