[R] Looping over graphs in igraph

Nick Sabbe nick.sabbe at ugent.be
Fri May 6 09:33:52 CEST 2011


Hi Danielle.

You appear to have two problems:
1) getting the data into R
Because I don't have the file at hand, I'm going to simulate reading it
through a text connection
orgdata<-textConnection("Graph ID | Vertex1 | Vertex2 | weight\n1 | Alice |
Bob | 2\n1 | Alice | Chris | 1\n1 | Alice | Jane | 2\n1 | Bob | Jane | 2\n1
| Chris | Jane | 3\n2 | Alice | Tom | 2\n2 | Alice | Kate | 1\n2 | Kate |
Tom | 3\n2 | Tom | Mike | 2")
dfr <-read.table(orgdata, header=TRUE, sep="|", as.is=TRUE, strip.whit=TRUE)

For you, this would probably be more like
dfr <-read.table("somepath/fileOfInterest.csv", header=TRUE, sep="|",
as.is=TRUE, strip.whit=TRUE)

2) performing actions per graph id
require(igraph)
result<-sapply(unique(dfr$Graph.ID), function(curID){
		#There may be more elegant ways of creating the graphs per
ID, but it works
		curDfr<- dfr[dfr$Graph.ID==curID,]
		g<-graph.edgelist(as.matrix(curDfr[,c("Vertex1",
"Vertex2")]))
		g<-set.edge.attribute(g, "weight", value= curDfr$weight)
		#return whatever information you're interested about, based
on graph object g
		#for now I'm just returning edge and vertex counts
		return(c(v=vcount(g), e=ecount(g)))
	})
colnames(result)<-unique(dfr$Graph.ID)
print(result)

HTH,


Nick Sabbe
--
ping: nick.sabbe at ugent.be
link: http://biomath.ugent.be
wink: A1.056, Coupure Links 653, 9000 Gent
ring: 09/264.59.36

-- Do Not Disapprove




-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On
Behalf Of Danielle Li
Sent: donderdag 5 mei 2011 22:25
To: r-help at r-project.org
Subject: [R] Looping over graphs in igraph

Hi,


I'm trying to do some basic social network analysis with igraph in R, but
I'm new to R and haven't been able to find documentation on a couple basic
things:

I want to run igraph's community detection algorithms on a couple thousand
small graphs but don't know how to automate igraph looking at multiple
graphs described in a single csv file.  My data look like something in ncol
format, but with an additional column that has an ID for which graph the
edge belongs in:


Graph ID | Vertex1 | Vertex2 | weight

1 | Alice | Bob | 2

1 | Alice | Chris | 1

1 | Alice | Jane | 2

1 | Bob | Jane | 2

1 | Chris | Jane | 3


2 | Alice | Tom | 2

2 | Alice | Kate | 1

2 | Kate | Tom | 3

2 | Tom | Mike | 2


so on and so forth for about 2000 graph IDs, each with about 20-40
vertices.  I've tried using the "split" command but it doesn't recognize my
graph id: ("object 'graphid' not found)--this may just be because I don't
know how to classify a column of a csv as an object.


Ultimately, I want to run community detection on each graph separately--to
look only at the edges when the graph identifier is 1, make calculations on
that graph, then do it again for 2 and so forth.  I suspect that this isn't
related to igraph specifically--I just don't know the equivalent command in
R for what in pseudo Stata code would read as:


forvalues i of 1/N {

temp_graph=subrows of the main csv file for which graphid==`i'

cs`i' = leading.eigenvector.community.step(temp_graph)
convert cs`i'$membership into a column in the original csv

}


I want the output to look something like:


Graph ID | Vertex1 | Vertex2 | weight | Vertex 1 membership | Vertex 2
membership | # of communities in the graph


1 | Alice | Bob | 2 | A | B | 2

1 | Alice | Chris | 1 | A | B | 2

1 | Alice | Jane | 2 | A | B | 2

1 | Bob | Jane | 2 | B | B | 2

1 | Chris | Jane | 3 | B | B | 2


 2 | Alice | Tom | 2 | A | B | 3

2 | Alice | Kate | 1 | A | C | 3

2 | Kate | Tom | 3 |  C | B | 3

2 | Tom | Mike | 2 | B | C | 3


Here, the graphs are treated completely separately so that community A in
graph 1 need not have anything to do with community A in graph 2.


I would really appreciate any ideas you guys have.


Thank you!

Danielle

	[[alternative HTML version deleted]]

______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list