How do I create a self loop in igraph R?

How do I add self loop

to the graph next to the change Adjacency matrix

that changes c(i,i)=1

, is there a function that does this in the package igraph

R

?

Edit : creating a graph:

  network=read.csv(file.choose())
  network[,1]=as.character(network[,1]) 
  network[,2]=as.character(network[,2])
  mygraph=graph.data.frame(network,directed=TRUE)
  E(mygraph)$weight=as.numeric(network[,3]) 

      

reproducible example:

karate <- graph.famous("Zachary")
E(karate)$weight <- 2
adjacency<-get.adjacency(karate, 
              attr="weight", edges=FALSE, names=TRUE)
for (i in 1:vcount(karate)){
      adjacency[i,i]<-1
    }
karate2<-graph.adjacency(adjacency, mode="directed", weighted=TRUE)

      

I am looking for a faster and easier solution, maybe this is a function.

+3


source to share


1 answer


To add a custom loop for each vertex in the example karate

, just do

karate[from=V(karate), to=V(karate)] <- 1

      



This will give you

enter image description here

+4


source







All Articles