Skip to content

Instantly share code, notes, and snippets.

@maurolepore
Created July 16, 2021 14:01
Show Gist options
  • Save maurolepore/6389e124f502771d629d940a25207254 to your computer and use it in GitHub Desktop.
Save maurolepore/6389e124f502771d629d940a25207254 to your computer and use it in GitHub Desktop.
# Scenario data source:
# https://github.com/2DegreesInvesting/CapitalMarketsPlatform/blob/master/data/scenario_data.rds
# commit: a72c75b

library(tidyverse)
set.seed(123)

raw <- readr::read_rds("data-raw/scenario_data.rds")
# if (interactive()) glimpse(raw)

# Learn the structure of the data
small <- slice_sample(raw, n = 2)

clean <- function(daita, pick = TRUE) {
  pick <- rlang::enquo(pick)
  data %>%
    tidyr::pivot_longer(
      where(is.double),
      names_to = "year",
      names_transform = list(year = as.integer)
    ) %>%
    dplyr::filter(!is.na(value)) %>%
    janitor::clean_names() %>%
    mutate(across(where(is.character), \(x) gsub(" ", "_", tolower(x)))) %>%
    filter(!!pick)
}

scenarios <- small %>%
  clean(between(year, 2020, 2023))
scenarios

# Create a fake dataset with the same structure
scenarios <- tibble::tribble(
   ~model, ~scenario,  ~region, ~variable, ~unit, ~year, ~value,
      "a",       "a",      "a",   "a|b|c",   "-", 2020L,      0,
      "a",       "a",      "a",   "a|b|c",   "-", 2021L,      0,
      "a",       "a",      "a",   "a|b|c",   "-", 2022L,      0,
      "a",       "a",      "a",   "a|b|c",   "-", 2023L,      0,
      "b",       "b",      "b",     "d|e",  "gw", 2020L,  10.01
)

providers <- function(data) {
  data %>% distinct(model, scenario)
}
providers(scenarios)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment