Skip to content

Reservas Internacionales #39

@Johan-rosa

Description

@Johan-rosa
URL <- "https://cdn.bancentral.gov.do/documents/estadisticas/sector-externo/documents/reservas_internacionales.xlsx"
file <- tempfile(fileext = ".xlsx")
download.file(url = URL, destfile = file, mode = "wb")


xopen::xopen(file)
headers <- readxl::read_excel(
  file, 
  skip = 5, 
  n_max = 3, 
  col_names = FALSE,
  col_types = "text"
) 

years <- headers[1, ] |>
  tidyr::pivot_longer(dplyr::everything(), names_to = "colum", values_to = "year") |>
  dplyr::select(-colum) |>
  tibble::rowid_to_column("column") |>
  tidyr::fill(year) |>
  dplyr::mutate(year = stringr::str_extract(year, "\\d{4}")) |> 
  dplyr::slice(-1)

variable <- headers[2, ] |>
  dplyr::mutate(dplyr::across(2, \(x) "RESERVAS")) |> 
  tidyr::pivot_longer(dplyr::everything(), names_to = "colum", values_to = "variable") |>
  dplyr::select(-colum) |>
  tibble::rowid_to_column("column") |>
  tidyr::fill(variable) |>
  dplyr::slice(-1)

modalidad <- headers[3, ] |>
  tidyr::pivot_longer(dplyr::everything(), names_to = "colum", values_to = "modalidad") |>
  dplyr::select(-colum) |>
  tibble::rowid_to_column("column") |>
  dplyr::slice(-1)

readxl::read_excel(file, skip = 8, n_max = 12, col_names = FALSE) |>
  tidyr::pivot_longer(-c(1), names_to = "column", values_to = "value") |>
  dplyr::mutate(column = readr::parse_number(stringr::str_extract(column, "\\d+"))) |>
  dplyr::rename(mes = 1) |>
  dplyr::left_join(years) |> 
  dplyr::left_join(variable) |>
  dplyr::left_join(modalidad) |>
  dplyr::mutate(
    dplyr::across(c(variable, modalidad), stringr::str_to_title),
    mes = databcrd::crear_mes(mes),
    date = lubridate::make_date(year, mes, 1)
  ) 

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions