3

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.

academic.user
  • 639
  • 2
  • 9
  • 28
  • Can you create a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example)? What exactly are you talking about here? Do you want to create a self loop for every existing vertex? How did you create the graph in the first place? – MrFlick May 06 '15 at 02:24
  • Would something as simple as `library(igraph); test <- graph.data.frame(data.frame(one=1:2,two=2:1))` do for a base for further exploring? – thelatemail May 06 '15 at 02:27
  • @MrFlick, yes I want to create a self loop for every existing vertex.please see the edit part – academic.user May 06 '15 at 02:33
  • 1
    @academic.user - your example isn't reproducible, because we don't have your file. Please see my comment as an example of a simple graph that is reproducible as a standalone item. – thelatemail May 06 '15 at 02:34
  • @thelatemail,this code will not gives us graph with self loops – academic.user May 06 '15 at 02:35
  • @academic.user - I know that, but it is a basic graph to which you could add self-loops, hence answering your question. – thelatemail May 06 '15 at 02:36
  • @thelatemail, I added a graph to my question, but the case is I am looking for a function which adds self loops to every node. – academic.user May 06 '15 at 02:42
  • @MrFlick, I added a graph, but is it what you asked for ? – academic.user May 06 '15 at 02:49

1 Answers1

4

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

MrFlick
  • 195,160
  • 17
  • 277
  • 295