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

18
我正在使用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

有任何帮助或指引吗?


我之前没有使用过igraph数据,但如果您能提供一个简单可重现的示例,我可能可以从igraph类中提取数据。 - Roman Luštrik
你是指igraph的软件包吗? - Spacedman
是的,igraph包。仍在努力掌握术语。 - biased_estimator
1个回答

33

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

如果你想实现前者:

> 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)

我实际上是在尝试做后者,但前者也非常有用。我正在尝试将一些计算出的净属性带回数据框以对它们进行逻辑分析。 - biased_estimator
顺便说一下,我喜欢你的博客! - biased_estimator

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接