View source: R/network-utils.R
| filter_nodes | R Documentation |
Filter nodes using dplyr-style expressions on any node column or centrality
measure. Returns a cograph_network object by default (universal format), or
optionally the same format as input when keep_format = TRUE.
filter_nodes(
x,
...,
.keep_edges = c("internal", "none"),
keep_format = FALSE,
directed = NULL
)
subset_nodes(
x,
...,
.keep_edges = c("internal", "none"),
keep_format = FALSE,
directed = NULL
)
x |
Network input: cograph_network, matrix, igraph, network, or tna object. |
... |
Filter expressions using any node column or centrality measure. Available variables include:
Examples: |
.keep_edges |
How to handle edges. One of:
|
keep_format |
Logical. If TRUE, return the same format as input (matrix returns matrix, igraph returns igraph, etc.). Default FALSE returns cograph_network (universal format). |
directed |
Logical or NULL. If NULL (default), auto-detect from matrix symmetry. Set TRUE to force directed, FALSE to force undirected. Only used for non-cograph_network inputs. |
A cograph_network object with filtered nodes. If keep_format = TRUE,
returns the same type as input (matrix, igraph, network, etc.).
filter_edges, splot, subset_nodes
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")
# Keep only high-degree nodes (returns cograph_network)
filter_nodes(adj, degree >= 3)
# Keep format: matrix in, matrix out
filter_nodes(adj, degree >= 3, keep_format = TRUE)
# Filter by node label
splot(filter_nodes(adj, label %in% c("A", "C")))
# Combine centrality and metadata filters
splot(filter_nodes(adj, degree >= 2 & label != "D"))
# With cograph_network (pipe-friendly)
net <- as_cograph(adj)
net |>
filter_edges(weight > 0.3) |>
filter_nodes(degree >= 2) |>
splot()
# With igraph (keep_format = TRUE returns igraph)
if (requireNamespace("igraph", quietly = TRUE)) {
g <- igraph::make_ring(5)
filter_nodes(g, degree >= 2, keep_format = TRUE) # Returns igraph
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.