首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >R: nel2igraph和PN.amalgamation - igraph没有正确生成

R: nel2igraph和PN.amalgamation - igraph没有正确生成
EN

Stack Overflow用户
提问于 2018-06-06 12:36:44
回答 1查看 109关注 0票数 2

我遇到了包shp2graph的问题。我想使用PN.amalgamation函数,它工作得很好(见下面)。之后,我想要创建一个igraph对象。在这里,代码无法做到这一点。我可以用每个未合并的shp2graph对象很好地创建igraph对象。

这里是我的示例代码,它主要是来自包shp2graph描述的副本粘贴。

代码语言:javascript
复制
library(igraph)
library(shp2graph)

data(ORN)
rtNEL<-readshpnw(ORN.nt, ELComputed=TRUE)
res.sl<-SL.extraction(rtNEL[[2]],rtNEL[[3]])
res.me<-ME.simplification(res.sl[[1]],res.sl[[2]],DegreeL=res.sl[[4]]) 
res.pn<-PN.amalgamation(res.me[[1]],res.me[[2]],DegreeL=res.me[[4]])
ptcoords<-Nodes.coordinates(res.pn[[1]])
plot(ORN.nt)
points(ptcoords, col="green")
plot(ORN.nt)
points(Nodes.coordinates(rtNEL[[2]]), col="red")

# igraph created from amalgamation is wrong
test <- nel2igraph(nodelist= res.pn[[1]], edgelist=res.pn[[2]], Directed = TRUE)
plot(test,vertex.size=1,edge.width=1,edge.arrow.size=0,vertex.label=NA)

# res.me is one step before amalgamation
test <- nel2igraph(nodelist= res.me[[1]], edgelist=res.me[[2]], Directed = TRUE)
plot(test,vertex.size=1,edge.width=1,edge.arrow.size=0,vertex.label=NA)

任何帮助都是非常感谢的。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-06 14:01:43

我发现这个bug在某种程度上存在于与igraph包的交互中。问题是PN.amalgamation创建的节点的标签不再是连续的,有些已经丢失,因为我们删除了它们。但是,igraph仍然试图创建它们,然后给出以下警告:

对于任何在这里遇到同样麻烦的人来说,这都是一种重新欺骗标签的方法。

创建您自己的nel2igraph函数:

代码语言:javascript
复制
nel2igraph_corr <- function (nodelist, edgelist, weight = NULL, eadf = NULL, Directed = FALSE) 
{


  nodes <- nodelist[, 1]
  Ne <- length(edgelist[, 1])
  Nn <- length(nodes)

    for (i in 1:Nn) {
      kk <- nodelist[i,][[1]]
      edgelist[which(edgelist[,c(2)]==kk),2] <- i
      edgelist[which(edgelist[,c(3)]==kk),3] <- i

      nodelist[i,][[1]] <- i
  }

    if (!is.null(weight)) {
    if (length(weight) != Ne && is.numeric(weight)) 
      stop("Please give right edge weight, which must be numeric and the same length as edges elment")
  }
  if (!is.null(eadf)) {
    if (length(eadf[, 1]) != Ne) 
      stop("The eadf must be numeric and the same length as edges elment")
  }



  gr <- graph.edgelist(unique(edgelist[, c(2, 3)]), directed = T)
  gr <- set.vertex.attribute(gr, "x", V(gr), Nodes.coordinates(nodelist)[,1])
  gr <- set.vertex.attribute(gr, "y", V(gr), Nodes.coordinates(nodelist)[, 
                                                                         2])
  gr.es <- E(gr)
  if (!is.null(weight)) 
    gr <- set.edge.attribute(gr, "weight", gr.es, weight)
  if (!is.null(eadf)) {
    eanms <- colnames(eadf)
    n <- length(eanms)
    for (i in 1:n) gr <- set.edge.attribute(gr, eanms[i], 
                                            gr.es, eadf[, i])
  }
  gr
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50720590

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档