A14 GUÍA DE TRABAJO SOBRE BASE DE DATOS DE COMERCIO EXTERIOR

0.1 Carga de librerías

library(readr)
library(dplyr)
library(tidyr)
library(kableExtra)

0.2 Carga de datos

# Carga directa del objeto de datos previamente procesado
load("C:/Users/MINEDUCYT/Documents/UES/CICLO VI/MÉTODOS DE LA INVESTIGACIÓN PARA ANÁLISIS ECONÓMICOS/Primer parcial/comercioexter/data_comercio_exterior.RData")

1 Punto 1 Base de Comercio exterior

formato_tabla <- ifelse(knitr::is_latex_output(), "latex", "html")

data_comercio_exterior %>% 
  head(10) %>% 
  kable(format = formato_tabla, caption = "Base de Comercio Exterior 2022-2026 (sin ISO)", align = "c") %>% 
  kable_styling(bootstrap_options = c("striped","hover","condensed","responsive"), 
                latex_options = c("striped","hold_position","scale_down"),
                full_width = TRUE) %>% 
  add_footnote(label = "Fuente: Elaboración propia con base en datos del BCR", notation = "symbol")
Table 1.1: Base de Comercio Exterior 2022-2026 (sin ISO)
pais sac anio mes valor_cif kilogramos_importaciones valor_fob kilogramos_exportaciones
AFGANISTAN 8708990000 2022 2 220.50 0.29 0 0
AFGANISTAN 8471900000 2022 5 665.90 4.00 0 0
AFGANISTAN 5702310000 2022 6 913.74 22.02 0 0
AFGANISTAN 5702502000 2022 6 566.35 11.36 0 0
AFGANISTAN 6111200000 2022 8 213.69 1.56 0 0
AFGANISTAN 8536509000 2022 9 42.28 2.00 0 0
AFGANISTAN 6210400000 2023 7 204.75 0.10 0 0
AFGANISTAN 0901210000 2024 4 0.00 0.00 440 21
AFGANISTAN 4909000000 2024 4 0.07 0.15 0 0
AFGANISTAN 8473300000 2024 6 60.18 1.00 0 0
* Fuente: Elaboración propia con base en datos del BCR

1.1 Carga de Funciones Especializadas

# Carga de script de funciones
source(
  file = "C:/Users/MINEDUCYT/Documents/UES/CICLO VI/MÉTODOS DE LA INVESTIGACIÓN PARA ANÁLISIS ECONÓMICOS/Primer parcial/comercioexter/funciones_comercio_exterior.R",
  echo = TRUE,
  max.deparse.length = 100,
  encoding = "utf-8"
)
## 
## > options(scipen = 999)
## 
## > library(dplyr)
## 
## > library(readxl)
## 
## > library(stringr)
## 
## > load("C:/Users/MINEDUCYT/Documents/UES/CICLO VI/MÉTODOS DE LA INVESTIGACIÓN PARA ANÁLISIS ECONÓMIC ..." ... [TRUNCATED] 
## 
## > nombre_archivo <- "C:/Users/MINEDUCYT/Documents/UES/CICLO VI/MÉTODOS DE LA INVESTIGACIÓN PARA ANÁL ..." ... [TRUNCATED] 
## 
## > nombres_iso_paises <- read_excel(nombre_archivo)
## 
## > nombres_iso_paises$nom_pais_esp <- toupper(nombres_iso_paises$nom_pais_esp)
## 
## > data_comercio_exterior <- data_comercio_exterior %>% 
## +     left_join(nombres_iso_paises, by = c(p .... [TRUNCATED] 
## 
## > herramientas_mostrar_paises <- function() {
## +     paises <- nombres_iso_paises %>% select(nom_pais .... [TRUNCATED] 
## 
## > herramientas_mostrar_regiones <- function() {
## +     nombres_iso_paises %>% group_by(region, cod_re .... [TRUNCATED] 
## 
## > herramientas_mostrar_paises_en_tabla <- function() {
## +     data_comercio_exterior %>% group_by(pai .... [TRUNCATED] 
## 
## > indicadores_IVCR_capitulo <- function(codigo_pais, 
## +     capitulo, anio) {
## +     codigo_pais <- e .... [TRUNCATED] 
## 
## > indicadores_IVCR_partida <- function(codigo_pais, 
## +     partida, anio) {
## +     codigo_pais <- enq .... [TRUNCATED] 
## 
## > indicadores_Balassa_capitulo <- function(codigo_pais, 
## +     capitulo, anio, normalizado = TRUE) { .... [TRUNCATED] 
## 
## > indicadores_Balassa_partida <- function(codigo_pais, 
## +     partida, anio, normalizado = TRUE) {
## + .... [TRUNCATED] 
## 
## > indicadores_IHH_Herfindahl_Hirschmann_anual_texto <- function(codigo_pais, 
## +     anio, normalizad .... [TRUNCATED] 
## 
## > indicadores_IHH_Herfindahl_Hirschmann_anual <- function(codigo_pais, 
## +     anio, normalizado = TR .... [TRUNCATED]

2 Punto 2 Información estandarizada de los países

data_comercio_exterior %>% 
  head(10) %>% 
  kable(format = formato_tabla, caption = "Base de Comercio Exterior con información estandarizada (ISO)", align = "c") %>% 
  kable_styling(bootstrap_options = c("striped","hover","condensed","responsive"), 
                latex_options = c("striped","hold_position","scale_down"),
                full_width = TRUE) %>% 
  add_footnote(label = "Fuente: Elaboración propia con base en datos del BCR", notation = "symbol")
Table 2.1: Base de Comercio Exterior con información estandarizada (ISO)
pais sac anio mes valor_cif kilogramos_importaciones valor_fob kilogramos_exportaciones nom_pais_ingles iso_2 iso_3 codigo_pais region cod_region sub_region cod_sub_region region_intermedia cod_region_intermedia
AFGANISTAN 8708990000 2022 2 220.50 0.29 0 0 Afghanistan AF AFG 4 Asia 142 Asia Meridional 34 NA NA
AFGANISTAN 8471900000 2022 5 665.90 4.00 0 0 Afghanistan AF AFG 4 Asia 142 Asia Meridional 34 NA NA
AFGANISTAN 5702310000 2022 6 913.74 22.02 0 0 Afghanistan AF AFG 4 Asia 142 Asia Meridional 34 NA NA
AFGANISTAN 5702502000 2022 6 566.35 11.36 0 0 Afghanistan AF AFG 4 Asia 142 Asia Meridional 34 NA NA
AFGANISTAN 6111200000 2022 8 213.69 1.56 0 0 Afghanistan AF AFG 4 Asia 142 Asia Meridional 34 NA NA
AFGANISTAN 8536509000 2022 9 42.28 2.00 0 0 Afghanistan AF AFG 4 Asia 142 Asia Meridional 34 NA NA
AFGANISTAN 6210400000 2023 7 204.75 0.10 0 0 Afghanistan AF AFG 4 Asia 142 Asia Meridional 34 NA NA
AFGANISTAN 0901210000 2024 4 0.00 0.00 440 21 Afghanistan AF AFG 4 Asia 142 Asia Meridional 34 NA NA
AFGANISTAN 4909000000 2024 4 0.07 0.15 0 0 Afghanistan AF AFG 4 Asia 142 Asia Meridional 34 NA NA
AFGANISTAN 8473300000 2024 6 60.18 1.00 0 0 Afghanistan AF AFG 4 Asia 142 Asia Meridional 34 NA NA
* Fuente: Elaboración propia con base en datos del BCR

3 Punto 3 Top 5 Ranking de Exportaciones

3.1 Creamos los Ranking por Year

#Seleccionar Años
anios_ranking <- 2022:2026
data_comercio_exterior %>% 
  filter(anio %in% anios_ranking) -> data_ranking

data_ranking %>% 
  group_by(anio, iso_3) %>% 
  summarise(total = sum(valor_fob)) %>% 
  mutate(percent = round(prop.table(total) * 100, 2)) %>% 
  slice_max(n = 5, order_by = total) %>% 
  as.data.frame() %>% 
  group_by(anio) %>% 
  mutate(rank = row_number(),
         data = paste(iso_3, "|", percent, sep = "")) %>% 
  select(anio, data, rank) %>% 
  as.data.frame() -> insumo_reporte

print(insumo_reporte)
##    anio      data rank
## 1  2022 USA|38.78    1
## 2  2022 GTM|17.05    2
## 3  2022 HND|16.65    3
## 4  2022  NIC|6.98    4
## 5  2022  CRI|4.27    5
## 6  2023  USA|35.6    1
## 7  2023 GTM|18.45    2
## 8  2023  HND|16.3    3
## 9  2023  NIC|7.85    4
## 10 2023  CRI|4.79    5
## 11 2024 USA|33.84    1
## 12 2024 GTM|19.53    2
## 13 2024 HND|16.14    3
## 14 2024  NIC|8.94    4
## 15 2024  CRI|4.71    5
## 16 2025 USA|32.45    1
## 17 2025 GTM|19.64    2
## 18 2025 HND|16.74    3
## 19 2025  NIC|9.12    4
## 20 2025  CRI|4.45    5
## 21 2026 USA|32.53    1
## 22 2026 GTM|19.46    2
## 23 2026 HND|15.44    3
## 24 2026   NIC|8.7    4
## 25 2026  CRI|4.51    5

3.2 Tabla en formato “RAW”

library(tidyr)

insumo_reporte %>% 
  pivot_wider(names_from = rank, values_from = data) -> mi_tabla

print(mi_tabla)
## # A tibble: 5 × 6
##    anio `1`       `2`       `3`       `4`      `5`     
##   <dbl> <chr>     <chr>     <chr>     <chr>    <chr>   
## 1  2022 USA|38.78 GTM|17.05 HND|16.65 NIC|6.98 CRI|4.27
## 2  2023 USA|35.6  GTM|18.45 HND|16.3  NIC|7.85 CRI|4.79
## 3  2024 USA|33.84 GTM|19.53 HND|16.14 NIC|8.94 CRI|4.71
## 4  2025 USA|32.45 GTM|19.64 HND|16.74 NIC|9.12 CRI|4.45
## 5  2026 USA|32.53 GTM|19.46 HND|15.44 NIC|8.7  CRI|4.51
mi_tabla %>%
  kable(format = formato_tabla,
        caption = "Top 5 Socios comerciales, periodo 2022-2026, datos en porcentaje de las exportaciones totales.",
        align = "c") %>% 
  kable_styling(latex_options = c("striped","hold_position","scale_down")) %>%
  add_footnote(label = "Fuente: Elaboración propia, con base en información del BCR")
Table 3.1: Top 5 Socios comerciales, periodo 2022-2026, datos en porcentaje de las exportaciones totales.
anio 1 2 3 4 5
2022 USA|38.78 GTM|17.05 HND|16.65 NIC|6.98 CRI|4.27
2023 USA|35.6 GTM|18.45 HND|16.3 NIC|7.85 CRI|4.79
2024 USA|33.84 GTM|19.53 HND|16.14 NIC|8.94 CRI|4.71
2025 USA|32.45 GTM|19.64 HND|16.74 NIC|9.12 CRI|4.45
2026 USA|32.53 GTM|19.46 HND|15.44 NIC|8.7 CRI|4.51
a Fuente: Elaboración propia, con base en información del BCR