sum_expand {migest} | R Documentation |
Sum bilateral data to include aggregate bilateral totals for origin and destination meta areas
Description
Expand matrix of data frame of migration data to include aggregate sums for corresponding origin and destination meta regions.
Usage
sum_expand(
m,
return_matrix = FALSE,
guess_order = TRUE,
area_first = TRUE,
orig_col = "orig",
dest_col = "dest",
flow_col = "flow",
orig_area_col = "orig_area",
dest_area_col = "dest_area",
orig_area = NULL,
dest_area = NULL
)
Arguments
m |
A |
return_matrix |
Logical to return a matrix. Default |
guess_order |
Logical to return a matrix or data frame ordered by origin and destination with area names at the end of each block. Default |
area_first |
Order area sums to be placed before the origin and destination values. Default |
orig_col |
Character string of the origin column name (when |
dest_col |
Character string of the destination column name (when |
flow_col |
Character string of the flow column name (when |
orig_area_col |
Character string of the origin area column name (when |
dest_area_col |
Character string of the destination area column name (when |
orig_area |
Vector of labels for the origin areas of each row of |
dest_area |
Vector of labels for the destination areas of each row of |
Value
A tibble
or matrix
with additional row and columns (for matrices) for aggregate sums for origin and destination meta-regions
Examples
##
## 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
##
## Not run:
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)
## End(Not run)