我正在研究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
我想在不同的阶段将存储在向量中的一些预先确定的元素添加到 2 级列表的给定子集中,其中子集列表与向量的大小相同。
问题是找到一种将元素添加到二级列表的有效方法。
更简单(也是迄今为止唯一)的方法是编写一个循环:
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 个),循环变得慢得多。这应该是一个“主力”功能,所以我想加快速度。
我阅读了“有效地在 R 中的向量或列表中添加或删除元素? ”的答案,即尽可能使用向量并预先分配它们的大小,但我认为它不适用于我的情况,因为:
- 我认为使用igraph我别无选择,只能使用列表(至少在第一级)
- 在第二级,列表将具有不同的最终长度,具体取决于随机选择的顶点。因此,很难预先分配正确大小的向量。即使我将非常大的向量放在第二层,最初用 NA 填充(导致向量列表),我也不知道在哪个位置添加元素(因为任何迭代时列表的长度都是随机的),更不用说我以后需要删除 NA。
这应该是添加元素(使用)嵌套列表的特殊情况。因此,我认为也许可以通过用ddply
in plyr or替换内部循环来实现更快的实现do.call
,但我无法编写要应用的函数:get the elements of the (inner) list and add this new element (itself a subset of a vector)
任何意见或建议表示赞赏。希望帖子清楚。