我正在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
我想在不同阶段将存储在向量中的一些预先确定的元素添加到给定的二级列表子集,其中子集列表与向量的大小相同。
问题是找到一种有效的方法将元素添加到二级列表。
更简单(也是迄今为止唯一)的方法是编写一个循环:
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]))
}
}
(如果这是糟糕的编程实践的恐怖表演,我向有经验的读者道歉)
随着初始网络的大小变得越来越大,并且每次迭代都会选择更多的顶点(随机数,而不是 5),循环变得更慢。这应该是一个“主力”功能,所以我想加快它的速度。
我读到了“Efficiently adding or removing elements to a vector or list in R? ”的答案,即尽可能使用向量并预先分配它们的大小,但我认为它不适用于我的情况,因为:
- 我认为使用 igraph 我别无选择,只能使用列表(至少在第一级)
- 在第二级,列表将具有不同的最终长度,具体取决于随机选择的顶点。因此,很难预先分配正确大小的向量。即使我将非常大的向量放在第二层,最初填充了 NA(产生向量列表),我也不知道在哪个位置添加元素(因为任何迭代时列表的长度都是随机的),更不用说我稍后需要删除 NA。
这应该是添加元素(使用)嵌套列表的特殊情况。因此,我认为也许可以通过用 plyr 或 do.call
中的 ddply
替换内部循环来实现更快的实现,但是我无法编写要应用的函数:获取(内部)列表的元素并添加这个新元素(本身是向量的子集)
如有任何意见或建议,我们将不胜感激。希望这篇文章是清楚的。
最佳答案
# 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]]))
})
关于performance - 迭代且高效地将元素添加到 R igraph 中顶点的列表属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11959093/