简体   繁体   English

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

[英]Sort sub graph list in igraph by number of edges R

I have an igraph sub-graph cluster list as follows: 我有一个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

I want to sort tis subgraph list modules by the number of edges in each subgraph, (or based on the length of the sublists within it). 我想按每个子图中的边数(或根据其中的子列表的长度)对子图列表modules进行排序。

From the answer to this question: Sort a nested list by the length of the respective sublists 从这个问题的答案: 按相应子列表的长度对嵌套列表进行排序

I tried: 我试过了:

out <- modules[order(sapply(modules,length),decreasing=T)]

But this just gave back the same list: 但这只是返回了相同的列表:

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

This is because the sublists can only be accessed by using [[]] notation ( The difference between [] and [[]] notations for accessing the elements of a list or dataframe ). 这是因为只能通过使用[[]]表示法( 用于访问列表或数据帧元素的[]和[[]]表示法之间的区别)来访问子列表

The lengths of the sublists when evaluated using [] all equal 1, whereas the lengths of the sublists when evaluated using [[]] give the true length: 使用[]评估时,子列表的长度均等于1,而使用[[]]评估时,子列表的长度为真实长度:

length(modules[[1]])
[1] 10
> length(modules[1])
[1] 1

How can I modify the above to use the [[]] length rather than the [] length? 如何修改以上内容以使用[[]]长度而不是[]长度?


Updated to include full list and output: 更新以包括完整列表和输出:

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

Sorted output: 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 排序的输出:out> [[1]] IGRAPH 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: dput模块:

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"))

Having trawled through the igraph documentation it turns out you can use ecount to return the edge count for a particular sub-graph cluster. 遍历igraph文档后,您可以使用ecount返回特定子图群集的边数。

As such, doing: 因此,请执行以下操作:

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

gives the expected output: 给出预期的输出:

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

Documentation at: 文档位于:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM