首页 文章

将igraph对象转换为R中的数据框

提问于
浏览
14

我正在使用iGraph库,我需要在网络上运行一些统计分析 . 我正在使用iGraph计算几个变量,然后想要将这些指标用作几个回归中的因变量,将顶点属性用作模型中的自变量 .

所以,我能够加载数据,运行igraph分析,但是我无法将igraph对象转回数据框 . 我真的不需要保留边缘,只需将每个顶点转换为观察结果,每个行中的属性作为列 .

我尝试了以下方法:

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)

但它会引发以下错误:

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

任何帮助或指针将不胜感激 .

1 回答

  • 23

    我不太确定你要做什么 . 您希望将关系作为数据框,还是将节点属性作为数据框?

    做前者:

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

    做后者:

    > compg.df <- as.data.frame(list(Vertex=V(compg), Community=fccommunity, Hubscore=hubscore, Authscore=authscore), stringsAsFactors=FALSE)
    

相关问题