0

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 显示为属性。这是正常的吗?

G

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

4

1 回答 1

1

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

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 包(v0.3.0)于 2020 年 7 月 7 日创建

于 2020-07-06T12:05:02.870 回答