tool_expand_tibble: Expand tibble across scenarios and regions with default...

View source: R/tool_expand_tibble.R

tool_expand_tibbleR Documentation

Expand tibble across scenarios and regions with default values

Description

The data.frame 'd' is expanded in such a manner that all rows with 'NA' in either the 'scenario' or 'region' columns are extended to repeat for all scenarios and regions listed in 'scenarios' and 'regions'. Rows with specified scenarios and/or regions will overwrite extended ones. Regions are expanded before scenarios.

Usage

tool_expand_tibble(d, scenarios, regions, structure.columns = NULL)

Arguments

d

A data.frame with columns 'scenario' and 'region'.

scenarios

A character vector of scenario names.

regions

A character vector of region names.

structure.columns

A character vector of column names to be carried along.

Value

A 'tibble'.

Examples

## Not run: 
tribble(
  ~scenario,   ~region,   ~value,
  NA,          NA,        0,
  NA,          'CHA',     1,
  'SSP1',      NA,        2,
  'SSP2EU',    'DEU',     3) %>%
  tool_expand_tibble(scenarios = c('SSP1', 'SSP2EU', 'SSP5'),
                     regions = c('CHA', 'DEU', 'USA')) %>%
  pivot_wider(names_from = 'region')

tribble(
  ~scenario,   ~region,   ~name,   ~value,
  NA,          NA,        'A',     0,
  NA,          'CHA',     'B',     1,
  'SSP1',      NA,        'A',     2,
  'SSP2EU',    'DEU',     'B',     3) %>%
  tool_expand_tibble(scenarios = c('SSP1', 'SSP2EU', 'SSP5'),
                     regions = c('CHA', 'DEU', 'USA'),
                     structure.columns = 'name')

## End(Not run)

pik-piam/mrremind documentation built on May 1, 2024, 2:12 a.m.