R igraph遍历边列表并获取属性
我试图写一个有效的方法来打印出两组之间的边缘。问题是,当我遍历下面的边缘时,我似乎不知道如何从边缘获取任何信息,谢谢R igraph遍历边列表并获取属性,r,igraph,R,Igraph,我试图写一个有效的方法来打印出两组之间的边缘。问题是,当我遍历下面的边缘时,我似乎不知道如何从边缘获取任何信息,谢谢 myG <- erdos.renyi.game(100, 10/100) E(myG)$weight <- runif(ecount(myG)) V(myG)$group <- ifelse(runif(100)>0.5,1,2) V(myG)$origIndex <- seq(1,vcount(myG)) V(myG)$label <-
myG <- erdos.renyi.game(100, 10/100)
E(myG)$weight <- runif(ecount(myG))
V(myG)$group <- ifelse(runif(100)>0.5,1,2)
V(myG)$origIndex <- seq(1,vcount(myG))
V(myG)$label <- paste(sample(LETTERS,vcount(myG), replace=TRUE), sample(LETTERS,vcount(myG), replace=TRUE),sample(LETTERS,vcount(myG), replace=TRUE),sep="-")
indices1 <- which(V(myG)$group == 1)
indices2 <- which(V(myG)$group == 2)
edgeIDsOfAllBetween <- myG[[indices1, indices2, edges=TRUE]]
uniqueEdgeIDs <- unique(unlist(edgeIDsOfAllBetween))
edgeList <- E(myG)[uniqueEdgeIDs]
rows <- length(edgeList)
if(rows>0){
for(r in 1:rows){
edgeIndex <- edgeList[r] #how to get the original index??
weight <- get.edge.attribute(myG, "weight", index= edgeIndex)
n1Index <- edgeList[r,1] #how to get the index of the first vertex????
n2Index <- edgeList[r,2] #how to get the index of the second vertex????
n1IndexisIN1 <- n1Index %in% indices1
n1 <- get.vertex.attribute(myG,"label",index = n1Index)
n2 <- get.vertex.attribute(myG,"label",index = n2Index)
if(n1IndexisIN1){
n1 <- paste("group1_",n1,sep="")
n2 <- paste("group2_",n2,sep="")
}else{
n1 <- paste("group2_",n1,sep="")
n2 <- paste("group1_",n2,sep="")
}
print(paste(n1, " ", n2, " weight: ", weight, sep=""))
}
}
myG我认为您误解了什么是edgeList
以及如何访问它
您尝试对其进行迭代:
for(r in 1:rows){
edgeIndex <- edgeList[r]
但这甚至不是边缘列表中的第一条边缘!它实际上是图中的边数1
我猜想您只是想迭代edgelist本身:
> for(edge in edgeList){
+ print(edge)
+ }
[1] 57
[1] 272
[1] 1
[1] 7
这些可能是您正在寻找的边索引
> E(myG)[57]
Edge sequence:
e
e [57] 34 -- 2
> V(myG)[34]$group
[1] 2
> V(myG)[2]$group
[1] 1
在这些边上进行简单循环,获取边的顶点并打印出两个组,每次打印1和2(或2和1):
> for(edge in edgeList){
verts = get.edge(myG,edge)
print(V(myG)[verts[1]]$group) ; print(V(myG)[verts[2]]$group)
}
> E(myG)[57]
Edge sequence:
e
e [57] 34 -- 2
> V(myG)[34]$group
[1] 2
> V(myG)[2]$group
[1] 1
> for(edge in edgeList){
verts = get.edge(myG,edge)
print(V(myG)[verts[1]]$group) ; print(V(myG)[verts[2]]$group)
}