【问题标题】:Convert igraph object to a data frame in R将 igraph 对象转换为 R 中的数据框
【发布时间】:2011-06-21 18:09:25
【问题描述】:

我正在使用 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 类中提取数据。
  • 你的意思是 igraph
  • 是的,igraph 包。仍在使用术语。

标签: r statistics igraph


【解决方案1】:

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

做前者:

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

【讨论】:

  • 我实际上是在尝试做后者,但前者也非常有用。我正在尝试将一些计算得到的网络属性带回数据框中,以对它们进行一些 logit 分析。
  • 顺便说一句,我喜欢你的博客!
猜你喜欢
  • 2023-03-17
  • 2021-10-06
  • 2012-08-25
  • 1970-01-01
  • 1970-01-01
  • 2015-02-17
  • 2011-05-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多