Convert a directed graph to an undirected graph by normalizing edges and aggregating duplicate connections.
Arguments
- graph_df
A data frame representing a directed graph including columns:
from,to, and (optionally)edge,FX,FY,TX,TY.- by
Link characteristics to preserve/not aggregate across, passed as a one-sided formula or character vector of column names. Typically this includes attributes like mode, type, or capacity to ensure that only edges with the same characteristics are aggregated.
- ...
Arguments passed to
collap()for aggregation across duplicated (diretional) edges. The defaults areFUN = fmeanfor numeric columns andcatFUN = fmodefor categorical columns. Select columns usingcolsor use argumentcustom = list(fmean = cols1, fsum = cols2, fmode = cols3)to map different columns to specific aggregation functions. You can weight the aggregation (usingw = ~ weight_col).
Value
A data frame representing an undirected graph with:
edge- Edge identifier (first value from duplicates)from- Starting node ID (normalized to be <to)to- Ending node ID (normalized to be >from)FX- Starting node X-coordinate (first value from duplicates)FY- Starting node Y-coordinate (first value from duplicates)TX- Ending node X-coordinate (first value from duplicates)TY- Ending node Y-coordinate (first value from duplicates)Aggregated columns
Details
This function converts a directed graph to an undirected graph by:
Normalizing edge directions so that
from < tofor all edgesCollapsing duplicate edges (same
fromandtonodes)For spatial/identifier columns (
edge,FX,FY,TX,TY), taking the first value from duplicatesFor aggregation columns,
collap()will be applied.
Examples
library(flownet)
# Convert segments to graph and make undirected
graph <- africa_segments |>
linestrings_from_graph() |>
linestrings_to_graph()
graph_undir <- create_undirected_graph(graph, FUN = "fsum")
# Fewer edges after removing directional duplicates
c(directed = nrow(graph), undirected = nrow(graph_undir))
#> directed undirected
#> 14358 11385
