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

#Indicaciones Generales: Presente en un reporte de Rmardown (use un tema de su preferencia) todas las operaciones que se indican en la tarea, debe incluir el archivo rmd, el pdf y el informe publicado en su cuenta de Rpubs.

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
library(readxl)

1.

#Con base en la información disponible en la “Base de Datos de Comercio Exterior” del BCR, incluida en el archivo .RData disponible para esta tarea, para los años 2022-2026. Genera una tabla tal como se mostró en las clases (aún no incluya los nombres ISO de los países). Muestre un head de 10 casos.

load("C:/Users/steph/OneDrive/Documentos/UES/CICLO II-2026/MAE/COMERCIO_EXTERIOR/data_comercio_exterior.RData")

anios_ranking <- 2022:2026
data_comercio_exterior %>%
  filter(anio %in% anios_ranking) -> data_ranking_comercio

head(data_ranking_comercio, 10) %>%
  kable(caption = "BASE DE DATOS COMERCIO EXTERIOR 2022-2026") %>% kable_minimal() %>%
  
  add_footnote(label="Elaboracion propia con datos del BCR",
  notation="symbol") %>%  kable_minimal()
BASE DE DATOS COMERCIO EXTERIOR 2022-2026
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
* Elaboracion propia con datos del BCR

2.

#Agregue la información estandarizada de los países (nombres iso de los países y regiones, etc, tal como se vio en clases). Muestre un head de 10 casos.

Nombres_paises <- "C:/Users/steph/OneDrive/Documentos/UES/CICLO II-2026/MAE/COMERCIO_EXTERIOR/nombres_iso_paises.xlsx"
Nombres_iso_paises <- read_xlsx(Nombres_paises)
data_ranking_comercio <- data_ranking_comercio %>%
  left_join(Nombres_iso_paises, by = c("pais" = "nom_pais_esp"))

head(data_ranking_comercio, 10)%>%
  kable(caption = "BASE DE DATOS COMERCIO EXTERIOR ISO PAISES 2022-2026")%>% kable_minimal() %>%
  add_footnote(label="Elaboracion propia con datos del BCR",
  notation="symbol") %>%  kable_minimal()
BASE DE DATOS COMERCIO EXTERIOR ISO PAISES 2022-2026
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 NA NA NA NA NA NA NA NA NA NA
AFGANISTAN 8471900000 2022 5 665.90 4.00 0 0 NA NA NA NA NA NA NA NA NA NA
AFGANISTAN 5702310000 2022 6 913.74 22.02 0 0 NA NA NA NA NA NA NA NA NA NA
AFGANISTAN 5702502000 2022 6 566.35 11.36 0 0 NA NA NA NA NA NA NA NA NA NA
AFGANISTAN 6111200000 2022 8 213.69 1.56 0 0 NA NA NA NA NA NA NA NA NA NA
AFGANISTAN 8536509000 2022 9 42.28 2.00 0 0 NA NA NA NA NA NA NA NA NA NA
AFGANISTAN 6210400000 2023 7 204.75 0.10 0 0 NA NA NA NA NA NA NA NA NA NA
AFGANISTAN 0901210000 2024 4 0.00 0.00 440 21 NA NA NA NA NA NA NA NA NA NA
AFGANISTAN 4909000000 2024 4 0.07 0.15 0 0 NA NA NA NA NA NA NA NA NA NA
AFGANISTAN 8473300000 2024 6 60.18 1.00 0 0 NA NA NA NA NA NA NA NA NA NA
* Elaboracion propia con datos del BCR

3.

#Obtenga un ranking, anual, de los 5 principales socios comerciales de El Salvador, para el periodo 2022-2026. Presente sus resultados en el siguiente formato: Top 5 Socios comerciales, periodo 2022-2026, datos en porcentaje de las exportaciones totales.

data_ranking_comercio %>% 
  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() -> ranking_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.
head(ranking_reporte, 10)
##    anio   data rank
## 1  2022 NA|100    1
## 2  2022  PCN|0    2
## 3  2022  MNP|0    3
## 4  2022  TON|0    4
## 5  2022  GUM|0    5
## 6  2023 NA|100    1
## 7  2023  TCA|0    2
## 8  2023  GUM|0    3
## 9  2023  TON|0    4
## 10 2024 NA|100    1

#Nota: ISO país | %Expor. Significa que debe aparecer el código iso_3 del país y el porcentaje de exportaciones totales de el Salvador para ese país, en el año en cuestión, las columnas representan el lugar en el ranking, por ejemplo: GTM | 25.7

ranking_reporte %>% 
pivot_wider(names_from = rank,values_from = data)-> tabla
head(tabla)
## # A tibble: 5 × 6
##    anio `1`    `2`   `3`   `4`   `5`  
##   <dbl> <chr>  <chr> <chr> <chr> <chr>
## 1  2022 NA|100 PCN|0 MNP|0 TON|0 GUM|0
## 2  2023 NA|100 TCA|0 GUM|0 TON|0 <NA> 
## 3  2024 NA|100 PCN|0 GUM|0 TCA|0 <NA> 
## 4  2025 NA|100 TCA|0 PCN|0 <NA>  <NA> 
## 5  2026 NA|100 PCN|0 TCA|0 <NA>  <NA>
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 NA|100 PCN|0 MNP|0 TON|0 GUM|0
2023 NA|100 TCA|0 GUM|0 TON|0 NA
2024 NA|100 PCN|0 GUM|0 TCA|0 NA
2025 NA|100 TCA|0 PCN|0 NA NA
2026 NA|100 PCN|0 TCA|0 NA NA

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