##
## from matrix
##
m <- block_matrix(x = 1:16, b = c(2,3,4,2))
m
# requires a vector of origin and destination areas
a <- rep(LETTERS[1:4], times = c(2,3,4,2))
a
sum_expand(m = m, orig_area = a, dest_area = a)
# place area sums after regions
sum_expand(m = m, orig_area = a, dest_area = a, area_first = FALSE)
##
## from large data frame
##
if (FALSE) {
library(tidyverse)
library(countrycode)
# download Abel and Cohen (2019) estimates
f <- read_csv("https://ndownloader.figshare.com/files/26239945")
cm <- c("CHI" = "Europe",
"SCG" = "Europe",
"SUD" = "Africa")
# 1990-1995 flow estimates
f %>%
filter(year0 == 1990) %>%
mutate(
orig_area = countrycode(sourcevar = orig, custom_match = cm,
origin = "iso3c", destination = "un.region.name"),
dest_area = countrycode(sourcevar = dest, custom_match = cm,
origin = "iso3c", destination = "un.region.name")
) %>%
sum_expand(flow_col = "da_pb_closed", return_matrix = FALSE)
# by group (period)
f %>%
mutate(
orig_area = countrycode(sourcevar = orig, custom_match = cm,
origin = "iso3c", destination = "un.region.name"),
dest_area = countrycode(sourcevar = dest, custom_match = cm,
origin = "iso3c", destination = "un.region.name")) %>%
group_by(year0) %>%
sum_expand(flow_col = "da_pb_closed", return_matrix = FALSE)
}
Run the code above in your browser using DataLab