Extract one of more ego network from an input graph

extract_egonet(x, vertices = NULL, order = 1, type = c("all", "out", "in"))

Arguments

x

input graph of class igraph or network

vertices

if NULL the egonetworks for each vertex in the graph are returned. If vertices is a single vertex or a vector, the ego networks for these specific vertices are returned. This is generally what you want.

order

the order of the resulting graph

type

Character constant, it specifies how to use the direction of the edges if a directed graph is analyzed. For ‘out’ only the outgoing edges are followed, so all vertices reachable from the source vertex in at most order steps are counted. For ‘"in"’ all vertices from which the source vertex is reachable in at most order steps are counted. ‘"all"’ ignores the direction of the edges. This argument is ignored for undirected graphs.

Value

a list of graphs of the same class as the input graph

Details

For each vertex in vertices, the ego network is returned. This includes the vertices that can be reached in order number of steps. The type argument determines whether edges should be followed as incoming, outgoing, or undirected (and are considered undirected anyway for an undirected graph.)

The output is a list of egonetworks, one for each ego vertex.

Examples

g <- igraph::graph_from_literal(One --+ Two +-+ Three +-- Four --+ Five +-- Six +-- Seven +-+ Eight +-+ One +-+ Five)
snafun::to_matrix(g)
#>       One Two Three Four Five Six Seven Eight
#> One     0   1     0    0    1   0     0     1
#> Two     0   0     1    0    0   0     0     0
#> Three   0   1     0    0    0   0     0     0
#> Four    0   0     1    0    1   0     0     0
#> Five    1   0     0    0    0   0     0     0
#> Six     0   0     0    0    1   0     0     0
#> Seven   0   0     0    0    0   1     0     1
#> Eight   1   0     0    0    0   0     1     0
extract_egonet(g, vertices = 1, order = 1, type = "in")[[1]] |> snafun::to_matrix()
#>       One Five Eight
#> One     0    1     1
#> Five    1    0     0
#> Eight   1    0     0
extract_egonet(g, vertices = 1, order = 1, type = "out")[[1]] |> snafun::to_matrix()
#>       One Two Five Eight
#> One     0   1    1     1
#> Two     0   0    0     0
#> Five    1   0    0     0
#> Eight   1   0    0     0
extract_egonet(g, vertices = 1, order = 1, type = "all")[[1]] |> snafun::to_matrix()
#>       One Two Five Eight
#> One     0   1    1     1
#> Two     0   0    0     0
#> Five    1   0    0     0
#> Eight   1   0    0     0
extract_egonet(g, vertices = 1, order = 2, type = "in")[[1]] |> snafun::to_matrix()
#>       One Four Five Six Seven Eight
#> One     0    0    1   0     0     1
#> Four    0    0    1   0     0     0
#> Five    1    0    0   0     0     0
#> Six     0    0    1   0     0     0
#> Seven   0    0    0   1     0     1
#> Eight   1    0    0   0     1     0
extract_egonet(g, vertices = 1, order = 2, type = "out")[[1]] |> snafun::to_matrix()
#>       One Two Three Five Seven Eight
#> One     0   1     0    1     0     1
#> Two     0   0     1    0     0     0
#> Three   0   1     0    0     0     0
#> Five    1   0     0    0     0     0
#> Seven   0   0     0    0     0     1
#> Eight   1   0     0    0     1     0
extract_egonet(g, vertices = 1, order = 2, type = "all")[[1]] |> snafun::to_matrix()
#>       One Two Three Four Five Six Seven Eight
#> One     0   1     0    0    1   0     0     1
#> Two     0   0     1    0    0   0     0     0
#> Three   0   1     0    0    0   0     0     0
#> Four    0   0     1    0    1   0     0     0
#> Five    1   0     0    0    0   0     0     0
#> Six     0   0     0    0    1   0     0     0
#> Seven   0   0     0    0    0   1     0     1
#> Eight   1   0     0    0    0   0     1     0

extract_egonet(g, vertices = c("One"), order = 2, type = "in")[[1]] |> snafun::to_matrix()
#>       One Four Five Six Seven Eight
#> One     0    0    1   0     0     1
#> Four    0    0    1   0     0     0
#> Five    1    0    0   0     0     0
#> Six     0    0    1   0     0     0
#> Seven   0    0    0   1     0     1
#> Eight   1    0    0   0     1     0
extract_egonet(g, vertices = c("One", "Three"), order = 2, type = "in")
#> [[1]]
#> IGRAPH cce60c6 DN-- 6 9 -- 
#> + attr: name (v/c)
#> + edges from cce60c6 (vertex names):
#> [1] One  ->Five  One  ->Eight Four ->Five  Five ->One   Six  ->Five 
#> [6] Seven->Six   Seven->Eight Eight->One   Eight->Seven
#> 
#> [[2]]
#> IGRAPH cce6104 DN-- 4 4 -- 
#> + attr: name (v/c)
#> + edges from cce6104 (vertex names):
#> [1] One  ->Two   Two  ->Three Three->Two   Four ->Three
#> 

g_n <- snafun::to_network(g)
extract_egonet(g_n, vertices = 1, order = 1, type = "in")[[1]] |> snafun::to_matrix()
#>       One Five Eight
#> One     0    1     1
#> Five    1    0     0
#> Eight   1    0     0
extract_egonet(g_n, vertices = 1, order = 1, type = "out")[[1]] |> snafun::to_matrix()
#>       One Two Five Eight
#> One     0   1    1     1
#> Two     0   0    0     0
#> Five    1   0    0     0
#> Eight   1   0    0     0
extract_egonet(g_n, vertices = 1, order = 1, type = "all")[[1]] |> snafun::to_matrix()
#>       One Two Five Eight
#> One     0   1    1     1
#> Two     0   0    0     0
#> Five    1   0    0     0
#> Eight   1   0    0     0
extract_egonet(g_n, vertices = 1, order = 2, type = "in")[[1]] |> snafun::to_matrix()
#>       One Four Five Six Seven Eight
#> One     0    0    1   0     0     1
#> Four    0    0    1   0     0     0
#> Five    1    0    0   0     0     0
#> Six     0    0    1   0     0     0
#> Seven   0    0    0   1     0     1
#> Eight   1    0    0   0     1     0
extract_egonet(g_n, vertices = 1, order = 2, type = "out")[[1]] |> snafun::to_matrix()
#>       One Two Three Five Seven Eight
#> One     0   1     0    1     0     1
#> Two     0   0     1    0     0     0
#> Three   0   1     0    0     0     0
#> Five    1   0     0    0     0     0
#> Seven   0   0     0    0     0     1
#> Eight   1   0     0    0     1     0
extract_egonet(g_n, vertices = 1, order = 2, type = "all")[[1]] |> snafun::to_matrix()
#>       One Two Three Four Five Six Seven Eight
#> One     0   1     0    0    1   0     0     1
#> Two     0   0     1    0    0   0     0     0
#> Three   0   1     0    0    0   0     0     0
#> Four    0   0     1    0    1   0     0     0
#> Five    1   0     0    0    0   0     0     0
#> Six     0   0     0    0    1   0     0     0
#> Seven   0   0     0    0    0   1     0     1
#> Eight   1   0     0    0    0   0     1     0
extract_egonet(g_n, vertices = c("One"), order = 2, type = "in")[[1]] |> snafun::to_matrix()
#>       One Four Five Six Seven Eight
#> One     0    0    1   0     0     1
#> Four    0    0    1   0     0     0
#> Five    1    0    0   0     0     0
#> Six     0    0    1   0     0     0
#> Seven   0    0    0   1     0     1
#> Eight   1    0    0   0     1     0
extract_egonet(g_n, vertices = c("One", "Three"), order = 2, type = "in")
#> $`1`
#>  Network attributes:
#>   vertices = 6 
#>   directed = TRUE 
#>   hyper = FALSE 
#>   loops = FALSE 
#>   multiple = FALSE 
#>   bipartite = FALSE 
#>   total edges= 9 
#>     missing edges= 0 
#>     non-missing edges= 9 
#> 
#>  Vertex attribute names: 
#>     vertex.names 
#> 
#> No edge attributes
#> 
#> $`3`
#>  Network attributes:
#>   vertices = 4 
#>   directed = TRUE 
#>   hyper = FALSE 
#>   loops = FALSE 
#>   multiple = FALSE 
#>   bipartite = FALSE 
#>   total edges= 4 
#>     missing edges= 0 
#>     non-missing edges= 4 
#> 
#>  Vertex attribute names: 
#>     vertex.names 
#> 
#> No edge attributes
#>