【问题标题】:How to convert large list object (class of igraph.vs) to a dataframe in R如何将大型列表对象(igraph.vs 类)转换为 R 中的数据框
【发布时间】:2020-07-06 11:14:13
【问题描述】:

g 是一个 igraph 对象。我希望找到派系(mylist),然后将这个大列表对象转换为数据框对象。即一列带有派系编号,另一列带有该派系的成员。

mylist = maximal.cliques(g)

# error here when converting to dataframe 
cliques_df = mylist %>% 
   map_df(as_tibble)

但是,代码会产生错误:Error in as.data.frame.default(value, stringsAsFactors = FALSE) : cannot coerce class ‘"igraph.vs"’ to a data.frame

编辑:

运行 vertex_attr_names(g) 会产生“NodeID”(因此 NodeID 是节点属性)。

但是,我的g(igraph 对象)似乎没有将 NodeID 显示为属性。这正常吗?

链接到数据文件: https://drive.google.com/drive/folders/14eiJhW499lMM5BKaU4Qau-B7ieZCrSKx?usp=sharing

【问题讨论】:

  • 也许这篇文章有帮助? igraph.vs into a dataframe
  • 我试过但没用,给出了错误“不是图形对象”。不确定,但可能是因为 g 是 igraph.vs 对象,但在使用 maximal.cliques() 后,它是一个大列表对象 - 我希望将其转换为数据框。
  • 那么请给我们一个可重现的例子来仔细看看。
  • 如果我输入dput(),似乎列表对象太长,无法在此处复制可重现的代码。有解决办法吗?

标签: r list dataframe clique


【解决方案1】:

更新: 在随附的示例中,您在数据框中拥有集团编号和集团成员。当您使用maximal.cliques(g) 时,属性name 被保留,但属性PaperID 似乎被删除。您必须为属性name 执行以下分配:V(g)$name <- NodeIds 并在第二个sapply 中使用attributes(x)$name。仔细查看随附的工作示例。我已经说明了问题。

library(igraph)
#> 
#> Attache Paket: 'igraph'
#> The following objects are masked from 'package:stats':
#> 
#>     decompose, spectrum
#> The following object is masked from 'package:base':
#> 
#>     union


g <- sample_gnp(100, 0.3)
NodeIds <- paste("A", 1:100, sep =":")
V(g)$name <- NodeIds
V(g)$PaperID <- NodeIds
mylist <- maximal.cliques(g)


clique_number <- sapply(mylist, length)
clique_members <- sapply(mylist, function(x) paste("'", attributes(x)$name, "'", collapse = ",", sep = ""))
str(clique_members)
#>  chr [1:2035] "'A:87','A:81','A:57'" "'A:87','A:81','A:77','A:69'" ...


# empty character vector!!! 
cliques_members2 <- sapply(mylist, function(x) paste("'", attributes(x)$PaperID, "'", collapse = ",", sep = ""))
str(cliques_members2)
#>  chr [1:2035] "''" "''" "''" "''" "''" "''" "''" "''" "''" "''" "''" "''" ...

cliques_df <- data.frame(cliqueNums = clique_number, cliqueMembs = clique_members)
head(cliques_df, n = 10)
#>    cliqueNums                 cliqueMembs
#> 1           3        'A:87','A:81','A:57'
#> 2           4 'A:87','A:81','A:77','A:69'
#> 3           3        'A:87','A:79','A:69'
#> 4           4 'A:87','A:79','A:75','A:51'
#> 5           4 'A:87','A:79','A:75','A:65'
#> 6           3        'A:87','A:69','A:91'
#> 7           3        'A:87','A:65','A:28'
#> 8           3        'A:87','A:65','A:17'
#> 9           3        'A:87','A:57','A:28'
#> 10          3        'A:87','A:57','A:46'

# checks:
mylist[1:10]
#> [[1]]
#> + 3/100 vertices, named, from c3ed415:
#> [1] A:87 A:81 A:57
#> 
#> [[2]]
#> + 4/100 vertices, named, from c3ed415:
#> [1] A:87 A:81 A:77 A:69
#> 
#> [[3]]
#> + 3/100 vertices, named, from c3ed415:
#> [1] A:87 A:79 A:69
#> 
#> [[4]]
#> + 4/100 vertices, named, from c3ed415:
#> [1] A:87 A:79 A:75 A:51
#> 
#> [[5]]
#> + 4/100 vertices, named, from c3ed415:
#> [1] A:87 A:79 A:75 A:65
#> 
#> [[6]]
#> + 3/100 vertices, named, from c3ed415:
#> [1] A:87 A:69 A:91
#> 
#> [[7]]
#> + 3/100 vertices, named, from c3ed415:
#> [1] A:87 A:65 A:28
#> 
#> [[8]]
#> + 3/100 vertices, named, from c3ed415:
#> [1] A:87 A:65 A:17
#> 
#> [[9]]
#> + 3/100 vertices, named, from c3ed415:
#> [1] A:87 A:57 A:28
#> 
#> [[10]]
#> + 3/100 vertices, named, from c3ed415:
#> [1] A:87 A:57 A:46

mylist[[22]]
#> + 5/100 vertices, named, from c3ed415:
#> [1] A:21 A:67 A:62 A:27 A:22
cliques_df[22, ]
#>    cliqueNums                        cliqueMembs
#> 22          5 'A:21','A:67','A:62','A:27','A:22'

reprex package (v0.3.0) 于 2020 年 7 月 7 日创建

【讨论】:

  • 非常感谢!这很有帮助。但是我确实想将“NodeID”(而不是索引)导入数据框,即集团的成员应该显示为 NodeID。虽然我的 igraph 对象 g 具有“NodeID”作为顶点属性,但当我应用 maximal.cliques() 时,mylist 似乎删除了 NodeID。有没有办法让它出现在数据框中?
  • 也许您可以使用这些属性。我会更新reprex。
  • 感谢您的代码。不幸的是,运行代码后,我的输出是“”(空)而不是 NodeID。我编辑了我的帖子以插入我的g 对象的屏幕截图,以防万一……在右侧的 R 环境栏中查看时,它不显示 NodeID。但是当我执行vertex_attr_names(g) 时,会显示nodeID。会不会是这个原因?
  • 这是一个相当大的网络。它运行了几分钟以获取maximal.cliques(g)。但我检查了你的代码。您所做的是使用attributes(x)$PaperID 插入第二个sapply。我的感觉是只有名为 name 的属性没有被删除。尝试执行以下分配:V(g)$name &lt;- V(g)$PaperID,然后在第二个sapply 中使用attributes(x)$name编辑:我已经在我的小示例中检查了它,并将更新 reprex,以便您可以看到它使用 attributes(x)$PaperID 失败。
  • 感谢您浏览它。现在可以了!我的学习点:所以“节点名称”需要分配给V(g)$name,并且这个命名不能改变(例如V(g)$ID会失败)。我想它适用于所有 igraph 对象。
猜你喜欢
  • 2020-04-03
  • 2017-12-17
  • 1970-01-01
  • 1970-01-01
  • 2022-06-27
  • 2023-01-26
  • 1970-01-01
  • 1970-01-01
  • 2019-08-04
相关资源
最近更新 更多