我正在研究 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?”给出的答案,即尽可能使用向量并预先分配它们的大小,但我认为它不适用于我的情况,因为:
这应该是添加元素(使用)嵌套列表的特殊情况。因此,我认为可以通过在 plyr 或ddply
中用do.call
替换内部循环来实现更快的实现,但我无法写入要申请的功能:get the elements of the (inner) list and add this new element (itself a subset of a vector)
任何评论或建议表示赞赏。希望这篇文章很清楚。
答案 0 :(得分: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]]))
})