我有一个igraph
对象,我使用以下内容创建:
g3 <- graph.data.frame(DF.WORK.EDGE, directed=TRUE, vertices=DF.WORK.VERTEX)
我使用以下方法应用max_cliques()
on图形对象:
print('max clique for prefer to work with measure is: ')
max_cliques(g3)
当然,我有一长串不同的集团子列表:
列表一直在继续,我需要找到哪个节点出现频率最高(重叠最多),所以我知道哪个节点在不同的子集团中。
如果没有add-hoc函数来总结这些值的频率,那么有人可以告诉我如何检索所有子列表并将它们连接在同一个列表中吗?我想在之后做一个汇总表,看看哪个号码在所有子列表中显示最多。谢谢!
你可以只unlist
和table
cliques
set.seed(8675309) ##Sets the random number generator so that results are exactly reproducible.
g <- graph_from_edgelist(matrix(sample(LETTERS[1:10], 50, replace=T), ncol = 2), directed = FALSE) ##made a random graph
cliques <- max_cliques(g) ##Find maximal cliques
#cliques is a list of vectors containing nodes
#unlist turns the list into one vector,
#a side effect of unlist is that the node ID is returned not the node
#name (in this case A-J) so we use the names function to grab the node names.
#Lastly table tabulates the data, in this case counts the instances of each node in the vector. table could be replaced by tapply, or any other number of function.
nodeCliques <- table(names(unlist(cliques)))
#To get the nodes that are in the most cliques we subset the table based on the max number of cliques the nodes are in.
nodeCliques[which(nodeCliques==max(nodeCliques))]
# H I
# 4 4
让我们做10个随机派:
set.seed(42)
n_cliques <- 10
clique_sizes <- sample(x = 20, size = n_cliques)
x <- lapply(seq_along(clique_sizes), function(i) {
sample(x = 50, clique_sizes[i])
})
x[1:3]
#> [[1]]
#> [1] 23 36 45 13 22 43 44 6 20 50 37 48 38 49 3 18 14 30 15
#>
#> [[2]]
#> [1] 42 37 39 19 32 1 49 45 9 38 25 15 17 2 36 16 33 30
#>
#> [[3]]
#> [1] 32 48 30 16 47 18
将它们放入数据帧:
d <- do.call(rbind, lapply(seq_along(x), function(i) {
data.frame(
clique = i,
vertex = x[[i]]
)
}))
head(d)
#> clique vertex
#> 1 1 23
#> 2 1 36
#> 3 1 45
#> 4 1 13
#> 5 1 22
#> 6 1 43
Vertex 30出现在7个派系中:
sort(table(d$vertex), decreasing = TRUE)[1:10]
#>
#> 30 1 32 36 38 37 50 8 11 14
#> 7 5 5 5 5 4 4 3 3 3
这些是7个派系:
subset(d, vertex == 30)$clique
#> [1] 1 2 3 4 6 7 9