View source: R/aggregate_map.R
aggregate_map | R Documentation |
Aggregates or disaggregates the values of a data frame according to a mapping
aggregate_map(
data,
mapping,
by,
subset2agg = NULL,
only.new = TRUE,
na.rm = TRUE,
weights = NULL,
forceAggregation = FALSE,
autodetect = "auto",
scaleWeights = TRUE,
variable = "variable",
value = "value",
unit = "unit",
weight_val_col = "weight_val_col",
weight_item_col = NULL,
fun = sum
)
data |
a data frame. |
mapping |
a data frame connecting the resolution in |
by |
(named) vector giving the correspondence between the column name
of |
subset2agg |
subset of variables for which the (dis)aggregation is
applied. If |
only.new |
If |
na.rm |
If |
weights |
a data frame, a variable name as a character vector, or
|
forceAggregation |
binary. If |
autodetect |
this parameter takes the values |
scaleWeights |
logical. If |
variable |
Column name of variables. Defaults to |
value |
Column name of values. Defaults to |
unit |
Column name of units. Defaults to |
weight_val_col |
name of the value column in the |
weight_item_col |
name of the item column in the |
fun |
aggregation function to use. Defaults to |
By default "weights"
is set to NULL
. For aggregations, this means that
values will be summed as they are. For disaggregations, each component of the
larger category will take the same value as the larger category, or
"scaleWeights"
is TRUE
, each component will be given an even weight. For
aggregations, "weights"
can also be the name of a variable contained in
"data"
. "weights"
may also be a data frame.
A data frame.
Antoine Levesque
library(tidyr)
library(dplyr)
data <- inline.data.frame(c(
"model; scenario; region; variable; unit; period; value",
"REMIND; Baseline; USA; GDP per Capita|MER; US$2005/yr; 2010; 40000",
"REMIND; Baseline; USA; GDP per Capita|MER; US$2005/yr; 2020; 50000",
"REMIND; Baseline; USA; Population; million; 2010; 300",
"REMIND; Baseline; USA; Population; million; 2020; 350",
"REMIND; Baseline; CHN; GDP per Capita|MER; US$2005/yr; 2010; 7000",
"REMIND; Baseline; CHN; GDP per Capita|MER; US$2005/yr; 2020; 8000",
"REMIND; Baseline; CHN; Population; million; 2010; 1300",
"REMIND; Baseline; CHN; Population; million; 2020; 1400"))
mapping = inline.data.frame(c(
"region; New_region",
"USA; GLO",
"CHN; GLO"
))
mapping2 = inline.data.frame(c(
"Item ; Item_new",
"Population; Urban Population ",
"Population; Rural Population"
))
weights = inline.data.frame(c(
"region; itemI ; weight",
"USA ; Urban Population; 0.5",
"USA ; Rural Population; 0.2",
"CHN ; Urban Population; 2",
"CHN ; Rural Population; 1"
))
#Regional Aggregation
aggregate_map(data,mapping, by = "region", subset2agg = c("Population"))
#Regional Weighted Aggregation
aggregate_map(data,mapping, by = "region", subset2agg = "GDP per Capita|MER",
weights = "Population")
#Variable Weigthed Disaggregation
aggregate_map(data,mapping2, by = c("variable" = "Item"),
subset2agg = c("Population"),weights = weights,
weight_val_col = "weight", weight_item_col = "itemI")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.