rigraphsna

extract ego_networks/sub-network in igraph


I have a graph of 169 vertices and 513 edges. I need to extract all ego_networks or sub_networks to get each node with its direct neighbours. I managed to do that using ego(graph) that produces each nodes with its direct neighbourhoods. However, the results of this functionn is a list of lists and could not extract each ego as a separate igraph or adjacency matrix object.

IS there a way to extract each ego_net as an igraph or adjacency matrix object?

sub1 <- ego(graph)

#sub1 is a list of lists that contain each nodes with its direct neighbours. 
I can access each ego network by sub1[1], sub1[2], ...etc. however, I could not extract each ego
as a separate graph object. 

Solution

  • Since you do not provide any data, I will illustrate the answer with the karate network found in the igraphdata package. As you noted, ego returns a list of nodes with their neighbors. So you just need to turn each of those lists into graphs. You can use lapply and induced_subgraph to do that.

    library(igraph)
    library(igraphdata)
    data(karate)
    sub1 <- ego(karate)
    
    ListOfGraphs = lapply(sub1, function(x) induced_subgraph(karate, x))
    class(ListOfGraphs[[1]])
    [1] "igraph"