如何将大型列表对象(igraph.vs 的 class)转换为 R 中的数据框

How to convert large list object (class of igraph.vs) to a dataframe in R

g 是一个 igraph 对象。我希望找到 cliques (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 显示为属性。这正常吗?

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

更新: 在附带的示例中,您在数据框中有集团编号和集团成员。当您使用 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-07-07 创建