2012-08-14 82 views
5

我工作在ř的信号传播算法,使用的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?”的答案,即与载体尽可能和预分配大小的工作,但我认为这并不适用于我的情况,这是因为:

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

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

的任何意见或建议非常感谢。希望这个帖子很明确。

+1

你需要在每次迭代执行一些''上g' igraph'操作(即做多填充'V(G)$ letters'其他东西),或者是好来填充所有的' V(g)$字母数据第一? – lockedoff 2012-08-14 19:35:47

+0

我会在一轮添加和另一轮之间执行其他操作,所以我不能一次性填充嵌套列表。 循环有点模糊,但我需要说明顶点的属性是逐层填充的。对困惑感到抱歉。 – MatteoS 2012-08-14 19:39:44

回答

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]])) 
}) 
+0

谢谢,但那不是我想要的。我很抱歉,如果它不明确,但我正在寻找一个函数,*添加*元素的内部列表,除*现有的*。如果我运行你的函数两次,第二次迭代*的内容替换*第一次。 无论如何,ddply语句是一个很好的起点。 你认为我需要让问题更清楚吗? – MatteoS 2012-08-14 19:30:05

+0

我想我遵循,看我的编辑,你可以结合,而不是在lapply内分配。 – Andy 2012-08-14 19:33:34

+0

是的,我注意到(并提到)我可以将属性分配给矢量,而不是列表。但是,由于每个顶点的最后一个字母的数量是不同的(参见我在代码中的问题和相关点),所以我认为列表是要走的路,但我可能会是这样。 – MatteoS 2012-08-14 19:34:13

相关问题