
Compute ratio indicators of summarized variables over macro indicators
compute_fastshare.Rdcompute_fastshare() summarizes selected numeric columns by specified groups,
merges the result with macroeconomic indicators, computes ratios
of summarized variables per macro variable, and returns either a
long or wide-format dataset.
Usage
compute_fastshare(
data,
macro_data = data.table::as.data.table(macro_indicators),
macro_cols,
cols,
group_cols,
fns,
output = c("long", "wide"),
groups = NULL
)Arguments
- data
A dataset containing the raw data to summarize.
- macro_data
A
data.frame,data.table, or tibble containing macro-level indicators. Must share at least one common grouping variable withdata. Default ismacro_indicators. The result will be returned in the same class as the input.- macro_cols
A character vector of column names in
macro_datato use as denominators for ratio calculations.- cols
A character vector of column names in
datato summarize.- group_cols
A character vector of column names in
datato group by. Typically includes country and date/year variables. Default isc("country_code", "year").- fns
A character vector of summary functions to apply to
cols. Examples:"sum","mean","median".- output
Either
"long"or"wide"(default"long")."long"returns a tidy table with columns: group variables,macro_var,summary_var,indicator, andvalue."wide"returns a table with one column per indicator and original macro/summary values.
- groups
Deprecated. Use
group_colsinstead.
Value
A dataset containing:
In
"long"format: group variables,macro_var,macro_value,summary_var,summary_value,indicator, andvalue.In
"wide"format: group variables, one column per indicator (summary_varpermacro_var), and original macro and summary values. The returned object will match the class of the inputdata.
Details
The function works as follows:
Summarizes
colsbygroupsusing the functions infns.Automatically detects common join variables between
summary_dtandmacro_data.Merges the summarized data with macro indicators.
Computes ratio indicators (
summary_value / macro_value) for all combinations of summarized columns and macro columns.Optionally reshapes the result into wide format with one column per indicator.
Examples
if (FALSE) { # \dontrun{
dt <- contract_harmonized |> mutate(year = year(est_date)) |> as.data.table()
compute_fastshare(
data = dt,
macro_cols = c("gdp_lcu", "pexpenditure_lcu"),
cols = c("gross_salary_lcu", "net_salary_lcu", "base_salary_lcu"),
fns = c("sum", "mean"),
output = "long",
groups = c("country_code", "year")
)
## produce results in wide format
compute_fastshare(
data = dt,
macro_cols = c("gdp_lcu", "pexpenditure_lcu"),
cols = c("gross_salary_lcu", "net_salary_lcu", "base_salary_lcu"),
fns = c("sum", "mean"),
output = "wide",
groups = c("country_code", "year"))
} # }