Extract a subgraph
extract_subgraph(x, v_to_keep, e_to_keep)
graph of same class as the input graph
Extract a subgraph from an input graph object by either choosing which vertices to keep or which edges to keep.
When both v_to_keep
and e_to_keep
are provided, only
v_to_keep
is used. Of course, the resulting graph can subsequently
be used as input to this function while providing e_to_keep
to further
prune the network.
When vertices are removed (through v_to_keep
), exactly the specified
vertices and all the already existing edges between them will be kept in the
resulting graph. Edges that lose their sender and/or receiver are removed
from the resulting graph.
Both v_to_keep
and e_to_keep
are numeric vectors and do not
take names.
The function works on both igraph
and network
and returns a subgraph
of that same class.
The underlying functions that perform the actual extraction are
induced_subgraph
,
subgraph.edges
, and
get.inducedSubgraph
.
Consult these functions for more detail and, in some cases, some more
functionality.
g <- igraph::make_ring(10)
extract_subgraph(g, v_to_keep = 3:8)
#> IGRAPH ceb0d5a U--- 6 5 -- Ring graph
#> + attr: name (g/c), mutual (g/l), circular (g/l)
#> + edges from ceb0d5a:
#> [1] 1--2 2--3 3--4 4--5 5--6
extract_subgraph(g, e_to_keep = 4:8)
#> IGRAPH ceb1af7 U--- 6 5 -- Ring graph
#> + attr: name (g/c), mutual (g/l), circular (g/l)
#> + edges from ceb1af7:
#> [1] 1--2 2--3 3--4 4--5 5--6
extract_subgraph(g, v_to_keep = 3:8, e_to_keep = 4:8)
#> Warning: When both 'v_to_keep' and 'e_to_keep' are specified, only 'v_to_keep' is used, to ensure result integrity.
#> IGRAPH ceb2682 U--- 6 5 -- Ring graph
#> + attr: name (g/c), mutual (g/l), circular (g/l)
#> + edges from ceb2682:
#> [1] 1--2 2--3 3--4 4--5 5--6
g <- to_network(g)
extract_subgraph(g, v_to_keep = 3:8)
#> Network attributes:
#> vertices = 6
#> directed = FALSE
#> hyper = FALSE
#> loops = FALSE
#> multiple = FALSE
#> bipartite = FALSE
#> total edges= 5
#> missing edges= 0
#> non-missing edges= 5
#>
#> Vertex attribute names:
#> vertex.names
#>
#> No edge attributes
extract_subgraph(g, e_to_keep = 4:8)
#> Network attributes:
#> vertices = 6
#> directed = FALSE
#> hyper = FALSE
#> loops = FALSE
#> multiple = FALSE
#> bipartite = FALSE
#> total edges= 5
#> missing edges= 0
#> non-missing edges= 5
#>
#> Vertex attribute names:
#> vertex.names
#>
#> No edge attributes
extract_subgraph(g, v_to_keep = 3:8, e_to_keep = 4:8)
#> Warning: When both 'v_to_keep' and 'e_to_keep' are specified, only 'v_to_keep' is used, to ensure result integrity.
#> Network attributes:
#> vertices = 6
#> directed = FALSE
#> hyper = FALSE
#> loops = FALSE
#> multiple = FALSE
#> bipartite = FALSE
#> total edges= 5
#> missing edges= 0
#> non-missing edges= 5
#>
#> Vertex attribute names:
#> vertex.names
#>
#> No edge attributes
data(emon, package = "network")
g <- emon$MtStHelens
extract_subgraph(g, e_to_keep = which(extract_edge_attribute(g, 'Frequency') == 2))
#> Network attributes:
#> vertices = 10
#> directed = TRUE
#> hyper = FALSE
#> loops = FALSE
#> multiple = FALSE
#> total edges= 12
#> missing edges= 0
#> non-missing edges= 12
#>
#> Vertex attribute names:
#> Command.Rank.Score Decision.Rank.Score Formalization Location Paid.Staff Sponsorship Volunteer.Staff vertex.names
#>
#> Edge attribute names:
#> Frequency
g_i <- to_igraph(g)
extract_subgraph(g_i, e_to_keep = which(extract_edge_attribute(g_i, 'weight') == 2))
#> IGRAPH ceed5cb DN-- 0 0 --
#> + attr: name (v/c), vertex.names (v/c), Sponsorship (v/c), Paid.Staff
#> | (v/n), Volunteer.Staff (v/n), Location (v/c), Formalization (v/n),
#> | Decision.Rank.Score (v/n), Command.Rank.Score (v/n), Frequency (e/n)
#> + edges from ceed5cb (vertex names):