academic.user
academic.user

Reputation: 679

How to create self loop in igraph R?

How to add self loop to a graph beside changing Adjacency matrix which is changing c(i,i)=1, is there a function to do that in igraph R package?

Edit : graph creation :

  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, mayebe a function to do that.

Upvotes: 4

Views: 1538

Answers (1)

MrFlick
MrFlick

Reputation: 206401

To add a self-loop for each vertex in the karate example, just do

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

This will give you

enter image description here

Upvotes: 4

Related Questions