GUIA_A14_GR16051

EJERCICIO SOBRE RANKING TOP 5 DE SOCIOS COMERCIALES

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)
## 
## Adjuntando el paquete: 'kableExtra'
## The following object is masked from 'package:dplyr':
## 
##     group_rows

1

load("C:/Users/carlo/Documents/Comercio_Exterior/data_comercio_exterior.RData")
head(data_comercio_exterior, 10) |>
kable(caption = "Catálogo de Países", align = "c") |>
  kable_material(html_font = "sans-serif") |>
 kable_styling(
    bootstrap_options = c("striped", "hover", "condensed", "responsive"), 
    full_width = TRUE, 
    position = "center"
  )
Catálogo de Países
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

2

library(readxl)
load("C:/Users/carlo/Documents/Comercio_Exterior/data_comercio_exterior.RData")
nombre_archivo<-"C:/Users/carlo/Documents/Comercio_Exterior/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 %>%
  left_join(nombres_iso_paises,
            by=c("pais"="nom_pais_esp"))-> data_comercio_exterior

data_comercio_original <- data_comercio_exterior
data_comercio_iso <- data_comercio_original %>% 
left_join(nombres_iso_paises, by = "codigo_pais", suffix = c("_comercio", "_iso"))
catalogo_paises <- data_comercio_iso %>% 
  distinct(codigo_pais, nom_pais_esp, iso_2_iso, iso_3_iso, region_iso, sub_region_iso)

  head(catalogo_paises, 10) %>% 
  kable(caption = "Catálogo de Países con Nombres ISO", align = "c") %>% 
  kable_material(html_font = "sans-serif") %>% 
  kable_styling(
    bootstrap_options = c("striped", "hover", "condensed", "responsive"), 
    full_width = TRUE, 
    position = "center"
  )
Catálogo de Países con Nombres ISO
codigo_pais nom_pais_esp iso_2_iso iso_3_iso region_iso sub_region_iso
4 AFGANISTAN AF AFG Asia Asia Meridional
8 ALBANIA AL ALB Europa Europa Del Sur
276 ALEMANIA DE DEU Europa Europa Oriental
20 ANDORRA AD AND Europa Europa Del Sur
24 ANGOLA AO AGO África Africa Sub-Sahariana
660 ANGUILA (R.U.) AI AIA Américas América Latina Y El Caribe
28 ANTIGUA Y BARBUDA AG ATG Américas América Latina Y El Caribe
530 ANTILLAS HOLANDESAS AN ANT Europa Europa Oriental
NA NA NA NA NA NA
12 ARGELIA DZ DZA África Africa Del Norte

3

#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
## `summarise()` has regrouped the output.
## ℹ Summaries were computed grouped by anio and iso_3.
## ℹ Output is grouped by anio.
## ℹ Use `summarise(.groups = "drop_last")` to silence this message.
## ℹ Use `summarise(.by = c(anio, iso_3))` for per-operation grouping
##   (`?dplyr::dplyr_by`) instead.
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
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(caption = paste("Top",5,"de Exportaciones periodo",
    min(anios_ranking),"-",max(anios_ranking))) %>%
  add_footnote(label = "Elaboración propia con base en datos del BCR")
Top 5 de Exportaciones periodo 2022 - 2026
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