我工作在ř的信号传播算法,使用的igraph(为随机图文库),其包括用2-工作级嵌套列表。迭代并有效地将元素添加到顶点的R中的igraph列表属性
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),循环变得越来越慢。这应该是一个“主力”功能,所以我想加快速度。
我读给“Efficiently adding or removing elements to a vector or list in R?”的答案,即与载体尽可能和预分配大小的工作,但我认为这并不适用于我的情况,这是因为:
- 我认为,随着我不得不在第二级使用列表(至少在第一级)
- ,这些列表将具有不同的最终长度,这取决于随机选择哪些顶点。因此,预先分配正确大小的矢量很困难。即使我在第二级放置了非常大的矢量,最初填充了NAs(导致了矢量列表),但我不知道在哪个位置添加元素(因为任何迭代中列表的长度都是随机的),更不用说我以后需要删除NAs。
这应该是添加元素(使用)嵌套列表的特殊情况。因此,我认为,也许更快的实现可以通过替换plyr或do.call
与ddply
内部循环来实现,但我不能管理自己写函数应用:get the elements of the (inner) list and add this new element (itself a subset of a vector)
的任何意见或建议非常感谢。希望这个帖子很明确。
你需要在每次迭代执行一些''上g' igraph'操作(即做多填充'V(G)$ letters'其他东西),或者是好来填充所有的' V(g)$字母数据第一? – lockedoff 2012-08-14 19:35:47
我会在一轮添加和另一轮之间执行其他操作,所以我不能一次性填充嵌套列表。 循环有点模糊,但我需要说明顶点的属性是逐层填充的。对困惑感到抱歉。 – MatteoS 2012-08-14 19:39:44