Removes duplicate edge rows from an edge list data frame and provides control over how self-loops (e.g., a -> a) are handled.
Usage
get_unique_edges(
edge_list_df,
self_loops = c("drop", "keep"),
from_col = "from",
to_col = "to"
)Arguments
- edge_list_df
A data frame representing the edge list, typically with columns "from" and "to" (or as specified by `from_col`, `to_col`).
- self_loops
A character string specifying how to handle self-loops. Must be one of "drop" (default) or "keep".
- from_col
Name of the source node column in `edge_list_df`. Default "from".
- to_col
Name of the target node column in `edge_list_df`. Default "to".
Value
A data frame with unique edges, with self-loops handled according to the `self_loops` argument. The from/to columns are coerced to character; all other columns in the input are preserved (first occurrence kept on dedup). If input columns are factors, they are converted to characters in the output.
Details
Any edge where either from or to is NA is automatically dropped before deduplication and self-loop handling.
Examples
edges <- data.frame(
from = c("A", "B", "A", "C", "D"),
to = c("B", "C", "B", "C", "D")
)
get_unique_edges(edges, self_loops = "drop")
#> from to
#> 1 A B
#> 2 B C
get_unique_edges(edges, self_loops = "keep")
#> from to
#> 1 A B
#> 2 B C
#> 3 C C
#> 4 D D
# With custom column names
edges_custom <- data.frame(
source = c("X", "Y", "X"),
target = c("Y", "Y", "Y")
)
get_unique_edges(edges_custom, from_col = "source", to_col = "target")
#> source target
#> 1 X Y
# With NAs (NAs are preserved as they are,
# duplicates involving NAs are also removed)
edges_na <- data.frame(
from = c("A", NA, "A", "B", NA),
to = c("B", "C", "B", "D", "C")
)
get_unique_edges(edges_na, self_loops = "keep")
#> from to
#> 1 A B
#> 2 B D
# No self-loops with NA to drop
get_unique_edges(edges_na, self_loops = "drop")
#> from to
#> 1 A B
#> 2 B D