【发布时间】:2015-01-06 22:42:21
【问题描述】:
我想知道是否可以在 igraph 中创建重叠社区?
df1 :顶点列表及其成员
EDIT:id是指顶点名称,membership是指顶点社区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 中创建这些重叠社区的解决方案是什么? 提前谢谢你,很抱歉我的长问题。
【问题讨论】:
-
igraph AFAIK 不支持此功能。
create.communities不适用于重叠社区。span> -
感谢@GaborCsardi 的评论,有没有其他方法可以让社区重叠?
-
我不确定您在这里要做什么,但您可以为您的社区定义自己的类。例如。只需将不同的组放在列表中即可。
-
@GaborCsardi,除非您指的是不属于任何社区的顶点,否则我没有收到您的评论。如果您为我澄清这一点,我将不胜感激。我想做的是有重叠的社区,我有顶点列表以及它们所属的社区类 ID。
-
是的,这就是我要说的。您已经在
df1中拥有您的社区。span>