Skip to content

Instantly share code, notes, and snippets.

@zackbatist
Last active January 20, 2018 23:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zackbatist/9244296 to your computer and use it in GitHub Desktop.
Save zackbatist/9244296 to your computer and use it in GitHub Desktop.
Some useful igraph commands (R) for converting weighted adjacency matrices to graph files
# Load adjacency matrix as a graph object
library(igraph)
Load adjacency matrix:
dat=read.csv(file.choose(),header=TRUE,row.names=1,check.names=FALSE)
m=as.matrix(dat)
g=graph.adjacency(m,mode="undirected",weighted=TRUE)
# Print a weighted adjacency matrix with names as columns and rows, just the upper half
get.adjacency(g, type=c("upper"),attr=NULL, names=TRUE, edges=TRUE)
# Write to .net file
write.graph(g, file="filename.net", format=c("pajek"))
# To write graphs to other formats see: http://igraph.sourceforge.net/doc/R/write.graph.html
# Edgelists are not weighted, as far I as know
# Community structure detection based on edge betweenness - Girvan and Newman 2004
gn=edge.betweenness.community (g, weights=E(g)$weight,directed=FALSE,edge.betweenness=TRUE,merges=TRUE,bridges=TRUE,modularity=TRUE,membership=TRUE)
# Community strucure via short random walks - Pons and Latapy 2005
w=walktrap.community(g, weights = E(g)$weight, steps = 4, merges =
TRUE, modularity = TRUE, membership = TRUE)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment