Skip to content

Commit

Permalink
Ensure missing edge attributes are filled with NA instead of NULL
Browse files Browse the repository at this point in the history
'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 se-sic#271.

Signed-off-by: Maximilian Löffler <[email protected]>
  • Loading branch information
maxloeffler committed Dec 3, 2024
1 parent 26859b1 commit 065b21a
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions util-networks.R
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down Expand Up @@ -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]])
}
Expand Down

0 comments on commit 065b21a

Please sign in to comment.