From 065b21a9c527101bb967597655514619d7b53cd0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20L=C3=B6ffler?= Date: Tue, 3 Dec 2024 16:46:19 +0100 Subject: [PATCH] Ensure missing edge attributes are filled with NA instead of NULL MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 'plyr::rbind.fill' uses NULL to fill missing values in lists. As we now use lists for most edge attributes, we need to handle this case separately to ensure missing values are filled with NAs instead. To fix this issue, we need to instantiate missing columns in dataframes with NAs before calling 'plyr::rbind.fill'. This operation is constant with respect to the amount of rows and should not impact performance too much. This works towards fixing #271. Signed-off-by: Maximilian Löffler --- util-networks.R | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/util-networks.R b/util-networks.R index 2e159fcc..a54129b4 100644 --- a/util-networks.R +++ b/util-networks.R @@ -1674,9 +1674,18 @@ merge.network.data = function(vertex.data, edge.data) { edge.data.filtered = Filter(function(ed) { return(nrow(ed) > 0) }, edge.data) - ## 2) call rbind + ## 2) add in missing columns + all.columns = Reduce(union, lapply(edge.data.filtered, colnames)) + edge.data.filtered = lapply(edge.data.filtered, function(edges) { + missing.columns = setdiff(all.columns, colnames(edges)) + for (column in missing.columns) { + edges[[column]] = NA + } + return(edges) + }) + ## 3) call rbind edges = plyr::rbind.fill(edge.data.filtered) - ## 3) correct empty results + ## 4) correct empty results if (is.null(edges)) { edges = create.empty.edge.list() } @@ -1807,7 +1816,7 @@ add.edges.for.bipartite.relation = function(net, bipartite.relations, network.co ## convert edge attributes to list similarely to 'convert.edge.attributes.to.list' edge.attrs = names(extra.edge.attributes) - which.attrs = !(edge.attrs %in% EDGE.ATTR.HANDLING) + which.attrs = !(edge.attrs %in% names(EDGE.ATTR.HANDLING)) for (attr in edge.attrs[which.attrs]) { extra.edge.attributes[[attr]] = as.list(extra.edge.attributes[[attr]]) }