Search code examples
rigraph

Removing All Edges in Igraph


I have the following network graph:

library(tidyverse)
library(igraph)


set.seed(123)
n=15
data = tibble(d = paste(1:n))

relations = tibble(
  from = sample(data$d),
  to = lead(from, default=from[1]),
)

graph = graph_from_data_frame(relations, directed=T, vertices = data) 

V(graph)$color <- ifelse(data$d == relations$from[1], "red", "orange")

plot(graph, layout=layout.circle, edge.arrow.size = 0.2)

enter image description here

I am trying to see how to remove "edges" from this graph.

I found the following code to remove "edges" (https://igraph.org/r/doc/igraph-minus.html):

#remove the "edge" between "node 14 and node 10"
g = graph
g <- g - edge("14|10")
plot(g)

Is there a quick way to remove all "edges" in this graph?

for (i in 1:15) {
    for (j in 1:15) {
       g <- g - edge("i|j")
    }
}  

But this is not working:

Error in as.igraph.vs(graph, vp) : Invalid vertex names

Is there a better way then to remove all the edges one by one?

Thank you!


Solution

  • If you want to remove all edges, you can use

    g <- graph-E(graph)
    plot(g)
    

    enter image description here