迭代且有效地将元素添加到R igraph中的顶点的列表属性中

Mat*_*eoS 5 performance r list igraph

我正在研究R中的信号传播算法,使用igraph(随机图库),它涉及使用2级嵌套列表.

Igraph允许将属性附加到顶点(图形的节点),这些可以是向量或列表,但在我的应用程序中,我需要嵌套列表.

要查看,请尝试:

library("igraph")
g <- graph.full(10) # create a fully connected graph with 10 vertices
V(g)$letters <- list(NULL) # adds a list called "letters" to every vertex
V(g)$letters # results in a nested list
Run Code Online (Sandbox Code Playgroud)

我想在不同的阶段将存储在向量中的一些预定元素添加到第二级列表的给定子集中,其中子集列表与向量的大小相同.

问题是找到一种将元素添加到二级列表的有效方法.

更简单(也是目前为止)的方法是编写一个循环:

set.seed(1234)

# every iteration represents a "round" of element addition ,
# followed by other operations. 
# So the attribute "letters" should not be populated in one sitting.
for (i in 1:10){

  # select randomly five 2nd-level lists (vertices) from the 1st-level list
  # the selected vertices are generated randomly for exposition, 
  # but I need to be able to select them from a well-defined vector (sel.ver)

  sel.vert <- sample(1:10, 5)

  # generate elements to add to the lists in the 2nd-level list (vertices)
  # again, i generate them randomly just to fill the vector, 
  #but the vector could be pre-determined

  add.elem <- sample(letters, 5)

  # now add add each element to its own list
  # notice that the first ELEMENT of add.elem (add.elem[1]) is added
  # to the attribute of the first SELECTED vertex (V(g)[sel.vert[1]]$letters,
  # the second element of add.elem with the second SELECTED vertex, and so on..

  for (l in 1:5){
    V(g)[sel.vert[l]]$letters <- list(c(V(g)[sel.vert[l]]$letters, add.elem[l]))    
  }
}
Run Code Online (Sandbox Code Playgroud)

(如果这是一个糟糕的编程实践的恐怖表演,我向有经验的读者道歉)

随着初始网络的大小变大并且在每次迭代时选择更多顶点(随机数而不是5),循环变得慢得多.这应该是一个"主力"功能,所以我想加快它.

我读到了" 有效地向R中的向量或列表添加或删除元素? " 的答案,即尽可能使用向量并预先分配它们的大小,但我认为它不适用于我的情况,因为:

  1. 我认为使用igraph我别无选择,只能使用列表(至少在第一级)
  2. 在第二级,列表将具有不同的最终长度,具体取决于随机选择的顶点.因此,很难预先分配正确大小的矢量.即使我在第二级放置了非常大的向量,最初填充了NA(导致向量列表),我不知道在哪个位置添加元素(因为在任何迭代中列表的长度是随机的),更不用说我以后需要删除NA.

这应该是添加元素(使用)嵌套列表的特殊情况.因此,我认为,也许更快的实现可以通过更换内部循环来实现ddplyplyrdo.call,但我不能管理自己写函数申请:get the elements of the (inner) list and add this new element (itself a subset of a vector)

任何评论或建议表示赞赏.希望这篇文章很清楚.

And*_*ndy 2

# number of vertices to add elements to at a time
nv <- 5

# selected vertices and elements
sel.ver <- sample(V(g), nv)
add.elem <- sample(letters, nv)

V(g)$letters[sel.ver] <- lapply(1:nv, function(x) {
  c(add.elem[x], unlist(V(g)$letters[sel.ver[x]]))
})
Run Code Online (Sandbox Code Playgroud)