1

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"))
Chuck
  • 3,664
  • 7
  • 42
  • 76
  • `out <- modules[order(sapply(modules,function(x) length(x[[1]])),decreasing=T)]` – Andrew Gustar May 26 '17 at 13:46
  • @AndrewGustar Same result :( – Chuck May 26 '17 at 13:48
  • If I do `modules <- list(a=list(c(1,2,3)),b=list(c(4,5,6,7)))` then `modules[order(sapply(modules,function(x) length(x[[1]])),decreasing=T)]` gives me `$b` before `$a`. Your structure must be different - can you post the output of `dput(modules[1:2])`? – Andrew Gustar May 26 '17 at 13:53
  • @AndrewGustar Please see my update, added to answer. It's not pretty :( – Chuck May 26 '17 at 13:58
  • I see what you mean! Try it with `function(x) length(x[[9]][[3]]$name)`. – Andrew Gustar May 26 '17 at 14:10
  • 1
    `out <- modules[order(sapply(modules,function(x) length(x[[9]][[3]]$name)))]` gives `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` I'm not sure why it doesn't work. You may have seen that I found a solution in which case, I'm really sorry to waste your time :( I really really appreciate that you tried to help. – Chuck May 26 '17 at 14:17
  • No problem - I think the other solution is much more sensible! – Andrew Gustar May 26 '17 at 14:22
  • @AndrewGustar Ah great - Thank you very much again! Have a nice day :) – Chuck May 26 '17 at 14:24

1 Answers1

2

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

Chuck
  • 3,664
  • 7
  • 42
  • 76