Commit a4b061a8 authored by Facundo Muñoz's avatar Facundo Muñoz ®️
Browse files

read_network(): aggregate volumes for multiple links

parent 71b6904b
Package: mapMCDA
Title: Produce an epidemiological risk map by weighting multiple risk
factors
Version: 0.4.45
Version: 0.4.47
Date: 2020-04-25
Authors@R: c( person("Andrea", "Apolloni", email =
"andrea.apolloni@cirad.fr", role = c("ctb"), comment = "Animal
......
......@@ -126,8 +126,23 @@ must be respected:",
## Check duplicated edges
if ( any(idx <- duplicated(dat[, 1:2])) ) {
print(dat[which(idx), 1:2])
stop("The links above appear more than once")
if (nc == 6L) {
## No volume. Just remove duplicated lines
dat <- dat[!duplicated(dat),]
} else {
## Aggregate volume with warning
warning(
"Aggregating volumes for multiple links.",
"If this is not what you want, stop now and aggregate manually."
)
dat <-
aggregate(
dat[,7],
by = dat[, 1:6],
FUN = "sum"
)
}
}
## Check graph is directed
......
......@@ -92,3 +92,33 @@ test_that("Informative errors for incorrectly formatted network data", {
"Expected 6 or 7 columns"
)
})
test_that("read_network aggregates volume for multiple links", {
base_df <- data.frame(
orig = rep(letters[1:4]),
dest = rep(letters[4:1]),
olon = rep(1:4),
olat = rep(1:4),
dlon = rep(4:1),
dlat = rep(4:1),
vol = 10*1:4,
stringsAsFactors = FALSE
)
df <- rbind(base_df, base_df, base_df)
tf <- tempfile(fileext = ".csv")
write.csv(df, file = tf, row.names = FALSE)
expect_warning(
res <- read_network(tf),
"Aggregating volumes"
)
expect_equal(
as_data_frame(res),
transform(base_df[, -(3:6)], vol = 3 * vol)[order(base_df$vol, decreasing = TRUE),],
check.attributes = FALSE
)
})
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment