我想知道是否有可能在igraph创建重叠社区?
df1:顶点列表及其成员资格
编辑:id引用顶点名称,成员资格是指顶点社区id,所以顶点1
是
属于两个社区:3
和2
df1<- structure(list(id = c(1L,2L,3L, 4L, 5L,6L, 7L, 8L,9L), membership = list(c(3, 2),c(2),c(1),c(4),
c(4, 1),c(2, 4),
c(1),c(1, 3),c(2,1,3))),
.Names = c("id","membership"),class = "data.frame", row.names = c(NA, -9L))
#id membership
#1 1 3, 2
#2 2 2
#3 3 1
#4 4 4
#5 5 4, 1
#6 6 2, 4
#7 7 1
#8 8 1, 3
#9 9 2, 1, 3
df2:具有边缘成员资格的边缘列表
df2<- structure(list(id1 = c(1L,2L,5L,8L,9L,4L,6L),id2 = c(3L,5L,8L,1L,2L,3L,7L), membership = list(c(1),c(2),c(1,4),c(3,2),
c(1,3, 4),c(3),c(1,2))),
.Names = c("id1","id2","membership"),class = "data.frame", row.names = c(NA, -7L))
#id1 id2 membership
#1 1 3 1
#2 2 5 2
#3 5 8 1, 4
#4 8 1 3, 2
#5 9 2 1, 3, 4
#6 4 3 3
#7 6 7 1, 2
请考虑我们要创建一个社区对象:
# diffrent membership vector from df1
mem <-sapply(1:max(sapply(df1$membership, function(x)length(x))),function(y){
unlist(lapply(df1$membership,function(x) x[y]))})
# [,1] [,2] [,3]
# [1,] 3 2 NA
# [2,] 2 NA NA
# [3,] 1 NA NA
# [4,] 4 NA NA
# [5,] 4 1 NA
# [6,] 2 4 NA
# [7,] 1 NA NA
# [8,] 1 3 NA
# [9,] 2 1 3
#create community objects for each membership vector
com<-sapply(1:ncol(mem),function(x) create.communities(membership=mem[,x], algorithm = NULL, merges = NULL,
modularity = NULL))
# com
#[[1]]
#Graph community structure calculated with the unknown algorithm
#Number of communities: 4
#Membership vector:
#[1] 3 2 1 4 4 2 1 1 2
# [[2]]
#Graph community structure calculated with the unknown algorithm
#Number of communities: NA
#Membership vector:
#[1] 2 NA NA NA 1 4 NA 3 1
#[[3]]
#Graph community structure calculated with the unknown algorithm
#Number of communities: NA
#Membership vector:
#[1] NA NA NA NA NA NA NA NA 3
**Edit**:graph=graph.data.frame(df2,directed=FALSE)
sapply (1:length(com),function(x) plot.communities(com[[x]],graph))
最后一行给我一个错误Error in membership(x) : Cannot calculate community membership
,因为它无法计算社区成员中包含NA的社区数量
那么,在igraph中创建这些重叠社区的解决方案是什么? 提前谢谢你,对不起,我很抱歉。
答案 0 :(得分:1)
df1
中列名称的含义。df2
。你为什么定义它?plot.communities
时出错。这是个人功能吗?graph
,因此也会产生错误。在任何情况下,当创建mem
,则首先使用的df1$membership
,然后,第二等对于一些(列2-4,7),有第一值membership
中只有一个元素,因此返回NA
。您可能必须明确检查元素的长度。
所以类似的东西可能就是你想要的(但如果你不得不替换ifelse
条件):
mem&lt; - sapply(1:max(sapply(df1 $ membership,length)),function(y)
sapply(df1 $ membership,function(x)
ifelse(length(x)&lt; y,x [1],x [y])))
&GT; MEM
[,1] [,2] [,3]
[1,] 3 2 3
[2,] 2 2 2
[3,] 1 1 1
[4,] 4 4 4
[5,] 4 1 4
[6,] 2 4 2
[7,] 1 1 1
[8,] 1 3 1
[9,] 2 1 3
我也略微改变了代码(例如摆脱了unlist(lapply
)。
编辑:
此外,在创建com
时,您可能需要lapply
而不是sapply
。