2

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 learned how to remove all the "edges" in this graph:

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

Now, I am trying to do this same thing, but using a "loop" instead:

for (i in 1:15)
 for (j in 1:15) { 

graph <- graph - edge(paste0(i, "|", j)) 

} 

But I think the problem is that the above code is trying to delete "edges" that exist, and this code does not have the ability to "skip" (override) an instance when it is commanded to delete a non-existing edge:

Error in delete_edges(e1, unlist(e2, recursive = FALSE)) : 
  At iterators.c:1828 : Cannot create iterator, invalid edge id, Invalid vertex id

Is there a way to instruct this "loop" to "skip" every instances where two edges do not have a connection and to continue until the loop is done?

Thank you!

stats_noob
  • 5,401
  • 4
  • 27
  • 83

1 Answers1

2

I don't know why you want to run a for loop, but please find below a possible solution using the as_edgelist() function from the igraph library.

Reprex

  • Your data
library(igraph)
library(dplyr)

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)

  • Suggested code
edgelist <- as_edgelist(graph, names = TRUE)

  for (i in 1:15) { 
    
    graph <- graph - edge(paste0(edgelist[i,1], "|", edgelist[i,2])) 
    
  } 

plot(graph)

Created on 2022-02-24 by the reprex package (v2.0.1)

lovalery
  • 4,524
  • 3
  • 14
  • 28
  • Thank you! I was just curious why my "loop" I had originally written is not working! – stats_noob Feb 24 '22 at 17:35
  • Just out of curiosity - why did you decide to use "reprex" package? Are there any advantages to using this? – stats_noob Feb 24 '22 at 17:36
  • If you are familiar with "igraph" in R - can check these out? https://stackoverflow.com/questions/71244872/zoom-and-hide-options-for-html-widgets – stats_noob Feb 24 '22 at 17:38
  • https://stackoverflow.com/questions/71243676/directly-adding-titles-and-labels-to-visnetwork – stats_noob Feb 24 '22 at 17:38
  • 1
    Hi @antonoyaro8, thank you very much for your feedback. I use the `reprex` library to make sure that the code I post on SO is properly reproducible. Regarding your other two questions, I will think about it but it is more related to the `visNetwork` library than to the `igraph` library and I am less comfortable (and have not enough time right now to dive in). I suggest you to add the tag `visnetwork` to your questions to increase your chances to be helped by the most expert people in this field. Cheers. – lovalery Feb 24 '22 at 17:50
  • 1
    Thank you so much for all your advice! – stats_noob Feb 24 '22 at 17:55