A more nuanced node selection function that improves upon filter_nodes()
with lazy centrality computation (only computes measures actually referenced),
multiple selection modes, and global context variables for structural awareness.
select_nodes(
x,
...,
name = NULL,
index = NULL,
top = NULL,
by = "degree",
neighbors_of = NULL,
order = 1L,
component = NULL,
keep_edges = c("internal", "none"),
keep_format = FALSE,
directed = NULL,
.keep_edges = NULL
)A cograph_network object with selected nodes. If keep_format = TRUE,
matrix, igraph, and statnet network inputs are converted back to that type.
Network input: cograph_network, matrix, igraph, network, or tna object.
Filter expressions using node columns, centrality measures, or global context variables. Centrality measures are computed lazily (only those actually referenced). Available variables:
All columns in the nodes dataframe: id, label,
name, x, y, inits, color, plus any custom
degree, indegree, outdegree,
strength, instrength, outstrength, betweenness,
closeness, eigenvector, pagerank, hub,
authority, coreness. Any other measure
centrality() computes can be named too; see
list_centralities().
component, component_size,
is_largest_component, neighborhood_size, k_core,
is_articulation, is_bridge_endpoint
is_isolated, is_source, is_sink,
is_leaf, is_cut, local_transitivity,
local_triangles
Character vector. Select nodes by name/label.
Integer vector. Select nodes by index (1-based).
Integer. Select top N nodes by centrality measure.
Character. Centrality measure for top selection. Default "degree".
Character or integer. Select neighbors of these nodes (by name or index).
Integer. Neighborhood order (1 = direct neighbors, 2 = neighbors of neighbors, etc.). Default 1.
Selection mode for connected components:
"largest"Select nodes in the largest connected component
Select nodes in component with this ID
Select component containing node with this name
How to handle edges. One of:
"internal"(default) Keep only edges between remaining nodes
"none"Remove all edges
Logical. If TRUE, matrix, igraph, and statnet network inputs are returned in that format. Default FALSE returns cograph_network.
Logical or NULL. If NULL (default), auto-detect.
Deprecated. Use keep_edges.
Selection modes are combined with AND logic (like tidygraph/dplyr):
select_nodes(x, top = 10, component = "largest") selects
top 10 nodes within the largest component
All criteria must be satisfied for a node to be selected
Centrality measures are computed lazily - only measures actually referenced
in expressions or the by parameter are computed. This makes
select_nodes() faster than filter_nodes() for large networks.
For networks with negative edge weights, betweenness,
closeness and pagerank are undefined and return NA,
with a cograph_negative_weights warning.
filter_nodes, select_neighbors,
select_component, select_top
adj <- matrix(c(0, .5, .8, 0, .5, 0, .3, .6,
.8, .3, 0, .4, 0, .6, .4, 0), 4, 4, byrow = TRUE)
rownames(adj) <- colnames(adj) <- c("A", "B", "C", "D")
select_nodes(adj, degree >= 3)
select_nodes(adj, top = 2, by = "pagerank")
select_nodes(adj, neighbors_of = "A", order = 2)
select_nodes(adj, component = "largest")
Run the code above in your browser using DataLab