Librerias necesarias

library(readr)
library(dplyr)
## 
## Adjuntando el paquete: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(tidyr)
library(kableExtra)
## Warning: package 'kableExtra' was built under R version 4.5.3
## 
## Adjuntando el paquete: 'kableExtra'
## The following object is masked from 'package:dplyr':
## 
##     group_rows

Base de Datos

load(r"(C:\Users\Rebeca\Desktop\BASE DE DATOS COMERCIO EXTERIOR\data_comercio_exterior.RData)")
source(
  file = "c:/Users/Rebeca/Desktop/BASE DE DATOS COMERCIO EXTERIOR/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/Rebeca/Desktop/BASE DE DATOS COMERCIO EXTERIOR/data_comercio_exterior.RData")
## 
## > nombre_archivo <- "C:/Users/Rebeca/Desktop/BASE DE DATOS COMERCIO EXTERIOR/nombres_iso_paises.xlsx ..." ... [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]

1 Matriz Geográfica general de Comercio Exterior

herramientas_mostrar_paises_en_tabla() %>% 
  head(10) %>% 
  kable(caption = "Matriz Geográfica Completa", align = "c") %>% 
  kable_material(html_font = "sans-serif")
## `summarise()` has grouped output by 'pais'. You can override using the
## `.groups` argument.
Matriz Geográfica Completa
pais anio # Transacciones
AFGANISTAN 2022 6
AFGANISTAN 2023 1
AFGANISTAN 2024 5
AFGANISTAN 2025 8
AFGANISTAN 2026 1
ALBANIA 2022 22
ALBANIA 2023 12
ALBANIA 2024 30
ALBANIA 2025 36
ALBANIA 2026 7

2 Matriz Geográfica Estandarizada

herramientas_mostrar_paises() %>% 
  head(10) %>% 
  kable(caption = "Catálogo de Países", align = "c") %>% 
  kable_material(html_font = "sans-serif")
Catálogo de Países
nom_pais_esp codigo_pais region cod_region region_intermedia cod_region_intermedia
AFGANISTAN 4 Asia 142 NA NA
ALBANIA 8 Europa 150 NA NA
ALEMANIA 276 Europa 150 NA NA
ANDORRA 20 Europa 150 NA NA
ANGOLA 24 África 2 África Central 17
ANGUILA (R.U.) 660 Américas 19 Caribe 29
ANTARTIDA 10 NA NA NA NA
ANTIGUA Y BARBUDA 28 Américas 19 Caribe 29
ANTILLAS HOLANDESAS 530 Europa 150 NA NA
ARABIA SAUDITA (SAU.) 682 Asia 142 NA NA

3 Ranking Anual de socios comerciales

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
## `summarise()` has grouped output by 'anio'. You can override using the
## `.groups` argument.
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.1 Top 5 de socios comerciales de El Salvador (2022-2026)

library(tidyr)

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

mi_tabla %>% 
  kable(caption = paste("Top 5 Socios comerciales, periodo", min(anios_ranking), "-", max(anios_ranking), ", datos en porcentaje de las exportaciones totales.")) %>% 
  add_footnote(label = "Elaboración propia con base en datos del BCR")
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

Note: aElaboración propia con base en datos del BCR