Chuck
Chuck

Reputation: 3852

Sort sub graph list in igraph by number of edges R

I have an igraph sub-graph cluster list as follows:

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

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:

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

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

Upvotes: 1

Views: 239

Answers (1)

Chuck
Chuck

Reputation: 3852

Having trawled through the igraph documentation it turns out you can use ecount to return the edge count for a particular sub-graph cluster.

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/gsize.html

Upvotes: 2

Related Questions