##
## 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/38016762", show_col_types = FALSE)
f
# 1990-1995 flow estimates
f %>%
filter(year0 == 1990) %>%
mutate(
orig_area = countrycode(sourcevar = orig, custom_dict = dict_ims,
origin = "iso3c", destination = "region"),
dest_area = countrycode(sourcevar = dest, custom_dict = dict_ims,
origin = "iso3c", destination = "region")
) %>%
sum_expand(flow_col = "da_pb_closed", return_matrix = FALSE)
# by group (period)
f %>%
mutate(
orig_area = countrycode(sourcevar = orig, custom_dict = dict_ims,
origin = "iso3c", destination = "region"),
dest_area = countrycode(sourcevar = dest, custom_dict = dict_ims,
origin = "iso3c", destination = "region")
) %>%
group_by(year0) %>%
sum_expand(flow_col = "da_pb_closed", return_matrix = FALSE)
}
Run the code above in your browser using DataLab