当未找到所有节点时

当未找到所有节点时

本文介绍了lapply函数在igraph中查找邻居(当未找到所有节点时)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为节点列表创建网络邻居的数据集.虽然我可以使用lapply函数来执行此操作,但我可以使用neighbors命令.更为复杂的是,我的某些查找节点不在图中,但是无论如何我都无法使其正常工作.

I am trying to create a data set of network neighbors for a list of nodes. I though I could do this with an lapply function where I use the neighbors command. As an added complication, some of my lookup nodes aren't in the graph, but I can't get it to work regardless.

这里是一个例子:

edgelist <- read.table(text = "
A B
B C
C D
D E
C F
F G")

testlist <- read.table(text = "
A
H
C
D
J")

testlist2 <- read.table(text = "
A
C
B
D
E")

library(igraph)
graph <- graph.data.frame(edgelist)
str(graph)

neighbors<- lapply(testlist2, function(p) {  #Each pledge_id
  temp=neighbors(graph,p)  #Find all the neighbors for that pledge
  return(temp)
})

neighbors<- lapply(testlist, function(p) {  #Each pledge_id
  temp=neighbors(graph,p)  #Find all the neighbors for that pledge
  return(temp)
})

不幸的是,在这两种情况下,这都会返回hogwash.我想念什么?

Unfortunately, this returns hogwash in both cases. What am I missing?

我想要的输出将是这样的:

My desired output would be something like this:

lookupnode neighbor
A    B
H    .
C    D
C    F
D    E
J    .

我知道最终我需要在某个地方添加temp = data.table :: rbindlist(temp)命令,但是我不认为这会造成浩劫.

I know eventually I need to add a temp=data.table::rbindlist(temp) command in somewhere, but I don't think that is causing the hogwash.

推荐答案

一件事是,您正在使用read.table函数创建data.frame并将该data.frame传递给lapply,以便对其进行迭代每个向量,而不是data.frameV1向量的元素.

One thing is that you're creating a data.frame with the read.table functions and passing in that data.frame to lapply so it's iterating over the each vector, not the elements of the V1 vector in the data.frame.

第二,该V1列是一个因素(对于因素提示,h/t为@Psidom).

Second, that V1 column is a factor (h/t to @Psidom for the factor hint).

第三,neighbors()函数将返回图顶点(根据我的估计),这些顶点需要进行迭代并返回name属性.

Third, the neighbors() function is going to return graph vertices which (from my reckoning) need to be iterated over and have the name attribute returned.

然后,按照您的建议,需要将它们rbind编入data.frame:

Then, as you suggest, these need to be rbinded into a data.frame:

get_neighbors <- function(graph, n) {

  do.call(rbind, lapply(n, function(x) {

    if (x %in% V(graph)$name) {

      nb <- neighbors(graph, x)

      if (length(nb) > 0) {
        data.frame(lookupnode=x,
                   neighbor=nb$name, # h/t @MrFlick for this shortcut
                   stringsAsFactors=FALSE)
      } else {
        data.frame(lookupnode=x, neighbor=NA, stringsAsFactors=FALSE)
      }

    } else {
      data.frame(lookupnode=x, neighbor=NA, stringsAsFactors=FALSE)
    }

  }))

}

get_neighbors(graph, as.character(testlist$V1))
##   lookupnode neighbor
## 1          A        B
## 2          H     <NA>
## 3          C        D
## 4          C        F
## 5          D        E
## 6          J     <NA>

get_neighbors(graph, as.character(testlist2$V1))
##   lookupnode neighbor
## 1          A        B
## 2          C        D
## 3          C        F
## 4          B        C
## 5          D        E
## 6          E     <NA>

我想知道Gabor是否可以在C端对neighbors()进行矢量化.

I wonder if Gabor can vectorize neighbors() on the C-side.

更新:

ego解决方案只有一点点不同:

The ego solution is only a tad different:

get_ego <- function(g, v, n=2) {
  do.call(rbind, lapply(v, function(x) {
    if (x %in% V(g)$name) {
      data.frame(node=x,
                 ego_n=sapply(ego(g, n, x), function(y) { V(g)[y]$name }),
                 stringsAsFactors=FALSE)
    } else {
      data.frame(node=x, ego_n=NA, stringsAsFactors=FALSE)
    }
  }))
}

这篇关于lapply函数在igraph中查找邻居(当未找到所有节点时)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 10:37