Convert igraph object to a data frame in R

不羁的心 提交于 2020-01-23 04:30:27

问题


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.


回答1:


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)


来源:https://stackoverflow.com/questions/4904972/convert-igraph-object-to-a-data-frame-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!