Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert igraph object to a data frame in R

I'm working with the iGraph library and I need to run some statistical analysis on the network. I'm computing several variables using iGraph and then want to use those indicators as the dependent variable in a few regressions and the vertex attributes as the independent variables in the model.

So, I'm able to load the data, run the igraph analysis, but I'm having trouble turning the igraph object back into a data frame. I don't really need the edges to be preserved, just each vertex to be turned into an observation with the attributes serving as a column in each row.

I tried the following:

fg <- fastgreedy.community(uncompg, merges=TRUE)
z<-which.max(fg$modularity)
fgc<- community.to.membership(uncompg, fg$merges,z)
names<-array(V(uncompg)$name)
fccommunity<-array(fgc$membership)
fcresult<-as.matrix(cbind(names,fccommunity))
compg <- set.vertex.attribute(compg, "community", value=fccommunity)

uncompg<-simplify(as.undirected(compg))
hubscore<-hub.score(compg)$vector
authscore<-authority.score(compg)$vector

netdata<-as.data.frame(compg)

But it throws the following error:

  cannot coerce class '"igraph"' into a data.frame

Any help or pointers would be greatly appreciated.

like image 240
biased_estimator Avatar asked Feb 05 '11 03:02

biased_estimator


1 Answers

I am not quite sure what you are trying to do. Do you want the relationships as a data frame, or the node attribute as a data frame?

To do the former:

> compg.edges <- as.data.frame(get.edgelist(compg))

To do the latter:

> compg.df <- as.data.frame(list(Vertex=V(compg), Community=fccommunity, Hubscore=hubscore, Authscore=authscore), stringsAsFactors=FALSE)
like image 57
DrewConway Avatar answered Sep 21 '22 18:09

DrewConway