首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >子组件在in中失败

子组件在in中失败
EN

Stack Overflow用户
提问于 2015-10-21 08:02:02
回答 1查看 424关注 0票数 0

我已经开始探索斯坦福的社会网络分析课程,在第二个实验室里,我遇到了一个与提供的代码有关的问题。

它涉及以下职能:

代码语言:javascript
复制
reachability <- function(g, m) {
  reach_mat = matrix(nrow = vcount(g), 
                     ncol = vcount(g))
  for (i in 1:vcount(g)) {
    reach_mat[i,] = 0
    this_node_reach <- subcomponent(g, (i - 1), mode = m)

    for (j in 1:(length(this_node_reach))) {
      alter = this_node_reach[j] + 1
      reach_mat[i, alter] = 1
    }
  }
  return(reach_mat)
}

现在,当将此函数应用于具有下列特征的图形对象krack_full

代码语言:javascript
复制
summary(krack_full)
 IGRAPH DN-- 21 232 -- 
 + attr: name (v/c), AGE (v/n), TENURE (v/n), LEVEL (v/n), DEPT (v/n), color (v/c), frame (v/c), advice_tie (e/n), friendship_tie (e/n),| reports_to_tie (e/n), color (e/c), arrow.size (e/n)

出现以下错误(通过回溯)

代码语言:javascript
复制
 Error in .Call("R_igraph_subcomponent", graph, as.igraph.vs(graph, v) -  : 
  At structural_properties.c:1244 : subcomponent failed, Invalid vertex id 
3 .Call("R_igraph_subcomponent", graph, as.igraph.vs(graph, v) - 
    1, as.numeric(mode), PACKAGE = "igraph") 
2 subcomponent(g, (i - 1), mode = m) 
1 reachability(krack_full, "in") 

您可以通过运行实验室1获得确切的数据。

知道怎么纠正这个错误吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-10-21 08:37:25

您使用了错误的索引(R索引基于1),这应该是正确的:

代码语言:javascript
复制
reachability <- function(g, m) {
  reach_mat = matrix(nrow = vcount(g), 
                     ncol = vcount(g))
  for (i in 1:vcount(g)) {
    reach_mat[i,] = 0
    this_node_reach <- subcomponent(g, i, mode = m) # used "i" instead of "(i - 1)"

    for (j in 1:(length(this_node_reach))) {
      alter = this_node_reach[j] # removed "+ 1"
      reach_mat[i, alter] = 1
    }
  }
  return(reach_mat)
}

顺便说一句,我认为您可以通过以下操作得到相同的可达矩阵:

代码语言:javascript
复制
# this returns the minimum distance between each node (=inf if not reachable)
distMatrix <- shortest.paths(krack_full, v=V(krack_full), to=V(krack_full))
# we set the values that are not infinite to 1
distMatrix[!is.infinite(distMatrix)] <- 1
# we set the values that are infinite to 0
distMatrix[is.infinite(distMatrix)] <- 0
# now distMatrix is your reachability matrix
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33253938

复制
相关文章

相似问题

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