问题
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.
回答1:
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

来源:https://stackoverflow.com/questions/30066255/how-to-create-self-loop-in-igraph-r