【发布时间】:2017-05-26 13:30:29
【问题描述】:
我有一个 igraph 子图簇列表如下:
head(modules,2)
[[1]]
IGRAPH UN-- 4 3 --
+ attr: name (v/c), value (e/n), sourceID (e/n), targetID (e/n)
+ edges (vertex names):
[1] 538--516 539--516 539--517
[[2]]
IGRAPH UN-- 5 6 --
+ attr: name (v/c), value (e/n), sourceID (e/n), targetID (e/n)
+ edges (vertex names):
[1] 293--274 293--291 293--290 293--275 274--290 274--275
我想按每个子图中的边数(或基于其中子列表的长度)对子图列表modules 进行排序。
来自这个问题的答案:Sort a nested list by the length of the respective sublists
我试过了:
out <- modules[order(sapply(modules,length),decreasing=T)]
但这只是返回了相同的列表:
head(out, 2)
[[1]]
IGRAPH UN-- 4 3 --
+ attr: name (v/c), value (e/n), sourceID (e/n), targetID (e/n)
+ edges (vertex names):
[1] 538--516 539--516 539--517
[[2]]
IGRAPH UN-- 5 6 --
+ attr: name (v/c), value (e/n), sourceID (e/n), targetID (e/n)
+ edges (vertex names):
[1] 293--274 293--291 293--290 293--275 274--290 274--275
这是因为子列表只能使用 [[]] 表示法 (The difference between [] and [[]] notations for accessing the elements of a list or dataframe) 访问。
使用[] 评估时子列表的长度都等于1,而使用[[]] 评估时子列表的长度给出真实长度:
length(modules[[1]])
[1] 10
> length(modules[1])
[1] 1
如何修改以上内容以使用[[]] 长度而不是[] 长度?
更新为包含完整列表和输出:
modules
>
[[1]]
IGRAPH UN-- 4 3 --
+ attr: name (v/c), value (e/n), sourceID (e/n), targetID (e/n)
+ edges (vertex names):
[1] 538--516 539--516 539--517
[[2]]
IGRAPH UN-- 5 6 --
+ attr: name (v/c), value (e/n), sourceID (e/n), targetID (e/n)
+ edges (vertex names):
[1] 293--274 293--291 293--290 293--275 274--290 274--275
[[3]]
IGRAPH UN-- 6 7 --
+ attr: name (v/c), value (e/n), sourceID (e/n), targetID (e/n)
+ edges (vertex names):
[1] 700--654 700--655 700--649 654--649 654--699 655--699 655--701
[[4]]
IGRAPH UN-- 4 3 --
+ attr: name (v/c), value (e/n), sourceID (e/n), targetID (e/n)
+ edges (vertex names):
[1] 575--578 574--580 578--580
[[5]]
IGRAPH UN-- 4 4 --
+ attr: name (v/c), value (e/n), sourceID (e/n), targetID (e/n)
+ edges (vertex names):
[1] 703--739 703--740 704--739 704--740
排序输出: 出去 > [[1]] 图 UN-- 4 3 -- + attr: 名称 (v/c), 值 (e/n), sourceID (e/n), targetID (e/n) +边(顶点名称): [1] 538--516 539--516 539--517
[[2]]
IGRAPH UN-- 5 6 --
+ attr: name (v/c), value (e/n), sourceID (e/n), targetID (e/n)
+ edges (vertex names):
[1] 293--274 293--291 293--290 293--275 274--290 274--275
[[3]]
IGRAPH UN-- 6 7 --
+ attr: name (v/c), value (e/n), sourceID (e/n), targetID (e/n)
+ edges (vertex names):
[1] 700--654 700--655 700--649 654--649 654--699 655--699 655--701
[[4]]
IGRAPH UN-- 4 3 --
+ attr: name (v/c), value (e/n), sourceID (e/n), targetID (e/n)
+ edges (vertex names):
[1] 575--578 574--580 578--580
[[5]]
IGRAPH UN-- 4 4 --
+ attr: name (v/c), value (e/n), sourceID (e/n), targetID (e/n)
+ edges (vertex names):
[1] 703--739 703--740 704--739 704--740
输入模块:
dput(modules)
list(structure(list(4, FALSE, c(2, 2, 3), c(0, 1, 1), c(0, 1,
2), c(0, 1, 2), c(0, 0, 0, 2, 3), c(0, 1, 3, 3, 3), list(c(1,
0, 1, 1), structure(list(), .Names = character(0)), structure(list(
name = c("538", "539", "516", "517")), .Names = "name"),
structure(list(value = c(0.7186, 0.7186, 0.7186), sourceID = c(2610235,
2610236, 2610236), targetID = c(2578681, 2578681, 2578682
)), .Names = c("value", "sourceID", "targetID"))), <environment>), class = "igraph"),
structure(list(5, FALSE, c(1, 2, 3, 4, 3, 4), c(0, 0, 0,
0, 1, 1), c(0, 1, 2, 4, 3, 5), c(0, 1, 2, 3, 4, 5), c(0,
0, 1, 2, 4, 6), c(0, 4, 6, 6, 6, 6), list(c(1, 0, 1, 3),
structure(list(), .Names = character(0)), structure(list(
name = c("293", "274", "291", "290", "275")), .Names = "name"),
structure(list(value = c(0.7286, 0.8785, 0.8785, 0.7286,
0.7286, 0.7786), sourceID = c(1442372, 1475074, 1475074,
1475074, 1442372, 1442372), targetID = c(1475074, 1467171,
1466635, 1448436, 1466635, 1448436)), .Names = c("value",
"sourceID", "targetID"))), <environment>), class = "igraph"),
structure(list(6, FALSE, c(1, 2, 3, 3, 4, 4, 5), c(0, 0,
0, 1, 1, 2, 2), c(0, 1, 2, 3, 4, 5, 6), c(0, 1, 2, 3, 4,
5, 6), c(0, 0, 1, 2, 4, 6, 7), c(0, 3, 5, 7, 7, 7, 7), list(
c(1, 0, 1, 5), structure(list(), .Names = character(0)),
structure(list(name = c("700", "654", "655", "649", "699",
"701")), .Names = "name"), structure(list(value = c(0.7078,
0.7078, 0.7078, 0.7744, 0.7078, 0.7078, 0.7078), sourceID = c(3062163,
3062164, 3090708, 3062163, 3062163, 3062164, 3062164),
targetID = c(3090708, 3090708, 3058341, 3058341,
3090707, 3090707, 3090709)), .Names = c("value",
"sourceID", "targetID"))), <environment>), class = "igraph"),
structure(list(4, FALSE, c(2, 3, 3), c(0, 1, 2), c(0, 1,
2), c(0, 1, 2), c(0, 0, 0, 1, 3), c(0, 1, 2, 3, 3), list(
c(1, 0, 1, 7), structure(list(), .Names = character(0)),
structure(list(name = c("575", "574", "578", "580")), .Names = "name"),
structure(list(value = c(0.7744, 0.7744, 0.7744), sourceID = c(2821538,
2821537, 2822504), targetID = c(2822504, 2824052, 2824052
)), .Names = c("value", "sourceID", "targetID"))), <environment>), class = "igraph"),
structure(list(4, FALSE, c(2, 3, 2, 3), c(0, 0, 1, 1), c(0,
2, 1, 3), c(0, 1, 2, 3), c(0, 0, 0, 2, 4), c(0, 2, 4, 4,
4), list(c(1, 0, 1, 9), structure(list(), .Names = character(0)),
structure(list(name = c("703", "704", "739", "740")), .Names = "name"),
structure(list(value = c(0.7744, 0.7744, 0.7744, 0.7744
), sourceID = c(3094741, 3094741, 3094742, 3094742),
targetID = c(3111079, 3111080, 3111079, 3111080)), .Names = c("value",
"sourceID", "targetID"))), <environment>), class = "igraph"))
【问题讨论】:
-
out <- modules[order(sapply(modules,function(x) length(x[[1]])),decreasing=T)] -
@AndrewGustar 同样的结果 :(
-
如果我做
modules <- list(a=list(c(1,2,3)),b=list(c(4,5,6,7)))然后modules[order(sapply(modules,function(x) length(x[[1]])),decreasing=T)]在$a之前给我$b。您的结构必须不同 - 您可以发布dput(modules[1:2])的输出吗? -
@AndrewGustar 请查看我的更新,添加到答案。这不漂亮:(
-
out <- modules[order(sapply(modules,function(x) length(x[[9]][[3]]$name)))]给Error in .Call("R_igraph_adjacent_vertices", graph, vv, mode, PACKAGE = "igraph") : At iterators.c:759 : Cannot create iterator, invalid vertex id, Invalid vertex id我不确定为什么它不起作用。你可能已经看到我找到了一个解决方案,在这种情况下,我真的很抱歉浪费你的时间:(我真的很感谢你试图提供帮助。