Tarea A14: Guía de trabajo sobre base de datos de Comercio Exterior

1 Carga y Procesamiento de Datos

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

1.1 Carga de la Base Consolidada

# Cargar el objeto de datos previamente procesado (.RData proporcionado para la tarea)
load("C:/Users/Paniagua/Documents/comercioexterior/data_comercio_exterior.RData")

1.2 Carga de Funciones Especializadas

source(
  file = "C:/Users/Paniagua/Documents/comercioexterior/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/Paniagua/Documents/comercioexterior/data_comercio_exterior.RData")
## 
## > nombre_archivo <- "C:/Users/Paniagua/Documents/comercioexterior/nombres_iso_paises.xlsx"
## 
## > 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 1: Base sin Estandarizar

# Tal como se mostró en clases, sin nombres ISO todavía
data_comercio_exterior %>% 
  head(10) %>% 
  kable(
    format = "html",
    caption = "El Salvador: Base Consolidada de Comercio Exterior (sin ISO)",
    align = "c"
  ) %>% 
  kable_styling(
    bootstrap_options = c("striped", "hover", "condensed", "responsive"),
    full_width = TRUE,
    position = "center"
  ) %>% 
  add_footnote
Table 2.1: El Salvador: Base Consolidada de Comercio Exterior (sin 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

3 Punto 2: Incorporación de Información Estandarizada de Países

# Catálogo geográfico (nombres ISO, países, regiones, etc.)
catalogo_paises <- herramientas_mostrar_paises_en_tabla()

# Unión de la base consolidada con el catálogo geográfico
# NOTA: ajustar el nombre de la llave si tu catálogo usa otro nombre de columna
# distinto a "pais" (por ejemplo "codigo_pais")
data_comercio_exterior_iso <- data_comercio_exterior %>% 
  left_join(catalogo_paises, by = "pais")

data_comercio_exterior_iso %>% 
  head(10) %>% 
  kable(
    format = "html",
    caption = "El Salvador: Base Consolidada de Comercio Exterior (con ISO)",
    align = "c"
  ) %>% 
  kable_styling(
    bootstrap_options = c("striped", "hover", "condensed", "responsive"),
    full_width = TRUE,
    position = "center"
  ) %>% 
  add_footnote
Table 3.1: El Salvador: Base Consolidada de Comercio Exterior (con ISO)
pais sac anio.x 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 anio.y # Transacciones
AFGANISTAN 8708990000 2022 2 220.5 0.29 0 0 Afghanistan AF AFG 4 Asia 142 Asia Meridional 34 NA NA 2022 6
AFGANISTAN 8708990000 2022 2 220.5 0.29 0 0 Afghanistan AF AFG 4 Asia 142 Asia Meridional 34 NA NA 2023 1
AFGANISTAN 8708990000 2022 2 220.5 0.29 0 0 Afghanistan AF AFG 4 Asia 142 Asia Meridional 34 NA NA 2024 5
AFGANISTAN 8708990000 2022 2 220.5 0.29 0 0 Afghanistan AF AFG 4 Asia 142 Asia Meridional 34 NA NA 2025 8
AFGANISTAN 8708990000 2022 2 220.5 0.29 0 0 Afghanistan AF AFG 4 Asia 142 Asia Meridional 34 NA NA 2026 1
AFGANISTAN 8471900000 2022 5 665.9 4.00 0 0 Afghanistan AF AFG 4 Asia 142 Asia Meridional 34 NA NA 2022 6
AFGANISTAN 8471900000 2022 5 665.9 4.00 0 0 Afghanistan AF AFG 4 Asia 142 Asia Meridional 34 NA NA 2023 1
AFGANISTAN 8471900000 2022 5 665.9 4.00 0 0 Afghanistan AF AFG 4 Asia 142 Asia Meridional 34 NA NA 2024 5
AFGANISTAN 8471900000 2022 5 665.9 4.00 0 0 Afghanistan AF AFG 4 Asia 142 Asia Meridional 34 NA NA 2025 8
AFGANISTAN 8471900000 2022 5 665.9 4.00 0 0 Afghanistan AF AFG 4 Asia 142 Asia Meridional 34 NA NA 2026 1

4 Punto 3: Ranking Anual del Top 5 de Socios Comerciales (2022-2026)

4.1 Cálculo del Ranking

anios_ranking <- 2022:2026

data_comercio_exterior_iso %>% 
  filter(anio.x %in% anios_ranking) -> data_ranking

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

print(insumo_reporte)
##    anio.x        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

4.2 Tabla en Formato “RAW”

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

print(mi_tabla)
## # A tibble: 5 × 6
##   anio.x `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

4.3 Tabla Final: Top 5 Socios Comerciales

mi_tabla %>% 
  kable(
    format = "html",
    caption = paste(
      "Top 5 Socios comerciales, periodo", min(anios_ranking), "-", max(anios_ranking),
      ", datos en porcentaje de las exportaciones totales."
    ),
    align = "c",
    col.names = c("Año", "1", "2", "3", "4", "5")
  ) %>% 
  kable_styling(
    bootstrap_options = c("striped", "hover", "condensed", "responsive"),
    full_width = TRUE,
    position = "center"
  ) %>% 
  add_footnote
Table 4.1: Top 5 Socios comerciales, periodo 2022 - 2026 , datos en porcentaje de las exportaciones totales.
Año 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