按边数 R 对 igraph 中的子图列表进行排序

Sort sub graph list in igraph by number of edges R

我有一个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]] 图联合国-- 4 3 -- + 属性:名称(v/c)、值(e/n)、源ID(e/n)、目标ID(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"))

通过搜索 igraph 文档发现您可以使用 ecount 到 return 特定子图集群的边数。

因此,做:

out <- modules[order(sapply(modules, ecount))]

给出预期的输出:

out
[[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-- 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

[[3]]
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

[[4]]
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

[[5]]
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

文档位于:

http://igraph.org/r/doc/sub-sub-.igraph.html

http://igraph.org/r/doc/gsize.html