更新:
在附加的示例中,您在数据框中拥有集团编号和集团成员。当您使用maximal.cliques(g)
该属性时,该属性name
被保留,但属性PaperID
似乎已被删除。您必须为属性执行以下分配name
:V(g)$name <- NodeIds
并attributes(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 日创建