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
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)
Some of these look wrong because links between distant nodes go behind other nodes of the opposite type.