From b50322cbec2d2d615ac7519ed25a553a1b3ca8af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20L=C3=B6ffler?= Date: Tue, 27 Aug 2024 17:08:04 +0200 Subject: [PATCH] Add 'remove.duplicate.edges' function MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add a function that takes a network as input and simply removes all edges that are exact duplicates of each other. Works towards #138. Signed-off-by: Maximilian Löffler --- util-networks.R | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/util-networks.R b/util-networks.R index da1b1da6..d917f94d 100644 --- a/util-networks.R +++ b/util-networks.R @@ -1993,6 +1993,43 @@ delete.isolates = function(network) { return(network.no.isolates) } +#' Remove duplicate edges from the given network. +#' +#' This function retains all set network, vertex, and edge attributes. +#' +#' @param network the given network +#' +#' @return the simplified network +remove.duplicate.edges = function(network) { + + logging::logdebug("remove.duplicate.edges: starting.") + + seen = list() + duplicates = c() + edges = igraph::as_data_frame(network, "edges") + + ## iterate over all edges and collect duplicates + for (i in seq_len(nrow(edges))) { + + ## get current edge without rowname + current.edge = edges[i, ] + rownames(current.edge) = NULL + + ## check whether the current edge is a duplicate + if (any(sapply(seen, function(edge) identical(edge, current.edge)))) { + duplicates = append(duplicates, i) + } else { + seen = append(seen, list(current.edge)) + } + } + + ## remove all duplicates + network = igraph::delete_edges(network, duplicates) + + logging::logdebug("remove.duplicate.edges: finished.") + return(network) +} + ## / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / ## Multi-network views -----------------------------------------------------