为二部图的一种模式的顶点设置向量属性
Setting vector attribute for vertices of one mode of a bipartite graph
我有一个二分图和一个数据框,其中每一行都与图第一侧的每个顶点相关联。该图未连接,当我找到该图的最大组成部分时,我必须对数据框进行子集化(这没有给我正确的答案)。另一种可能的选择是将数据框的行设置为图形第一侧顶点的属性(我不知道该怎么做)!这是一个玩具示例:
edgelist = matrix(c("A","a","A","b","B","b","C","c","D","c"),ncol=2,byrow=T)
bg <- graph.data.frame(edgelist, directed=F)
V(bg)$type <- V(bg)$name %in% edgelist[,1]
summary(bg)
V(bg)[V(bg)$type==1]
df = data.frame(id=c("A","B","C","D"), x=runif(4,10,50), y=sample(4), z=rnorm(4))
gclust = clusters(bg)
numClust = gclust$no
numLCC = gclust$csize[1]
bg2 = induced.subgraph(bg, which(gclust$membership ==which.max(gclust$csize)))
remained_set1 <- V(bg2)[V(bg2)$type==1]
df[as.character(df[,1])%in%remained_set1,] # wrong answer
尝试使用顶点的名称并翻转 %in%
subset(df, id %in% names(remained_set1))
这 returns 您的示例中 A 和 B 的行。
我有一个二分图和一个数据框,其中每一行都与图第一侧的每个顶点相关联。该图未连接,当我找到该图的最大组成部分时,我必须对数据框进行子集化(这没有给我正确的答案)。另一种可能的选择是将数据框的行设置为图形第一侧顶点的属性(我不知道该怎么做)!这是一个玩具示例:
edgelist = matrix(c("A","a","A","b","B","b","C","c","D","c"),ncol=2,byrow=T)
bg <- graph.data.frame(edgelist, directed=F)
V(bg)$type <- V(bg)$name %in% edgelist[,1]
summary(bg)
V(bg)[V(bg)$type==1]
df = data.frame(id=c("A","B","C","D"), x=runif(4,10,50), y=sample(4), z=rnorm(4))
gclust = clusters(bg)
numClust = gclust$no
numLCC = gclust$csize[1]
bg2 = induced.subgraph(bg, which(gclust$membership ==which.max(gclust$csize)))
remained_set1 <- V(bg2)[V(bg2)$type==1]
df[as.character(df[,1])%in%remained_set1,] # wrong answer
尝试使用顶点的名称并翻转 %in%
subset(df, id %in% names(remained_set1))
这 returns 您的示例中 A 和 B 的行。