1 Compile la información disponible en la “Base de Datos de Comercio Exterior” del BCR, para los años 2018-2020 y 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.

1.1 Importaciones

library(readr)
importaciones_2018_ene_jun <- read_delim("C:/Users/HEIDY ALVAREZ/Desktop/Proyecto MAE/Importaciones/2018-ene a jun.csv", 
    delim = "|", escape_double = FALSE, 
    trim_ws = TRUE)
head(importaciones_2018_ene_jun, 10)
library(dplyr)
library(readr)
library(data.table)
setwd("C:/Users/HEIDY ALVAREZ/Desktop/Proyecto MAE/Importaciones/")
nombres_archivos_importar <- list.files()
lista_importacion <- lapply(nombres_archivos_importar, FUN = read_delim, "|", col_names = c("pais", "sac", "anio","mes","valor_cif","kilogramos"), col_types = cols("c", sac="c",anio="d", mes = "d",valor_cif="d",kilogramos = "d"), skip=1)
data_importaciones <- bind_rows(lista_importacion)
head(data_importaciones,10)
data_importaciones %>% data.table()

1.2 Exportaciones

library(readr)
Exportaciones_2018_ene_jun <- read_delim("C:/Users/HEIDY ALVAREZ/Desktop/Proyecto MAE/Exportaciones/2018-ene a jun.csv", 
    delim = "|", escape_double = FALSE, 
    trim_ws = TRUE)
head(Exportaciones_2018_ene_jun, 10)
library(dplyr)
library(readr)
library(data.table)
setwd("C:/Users/HEIDY ALVAREZ/Desktop/Proyecto MAE/Exportaciones/")
nombres_archivos_importar <- list.files()
lista_exportacion <- lapply(nombres_archivos_importar, FUN = read_delim, "|", col_names = c("pais", "sac", "anio","mes","valor_fob","kilogramos"),col_types = cols(pais = "c", sac = "c", anio = "d",mes = "d", valor_fob ="d", kilogramos = "d"), skip = 1)
data_exportaciones <- bind_rows(lista_exportacion)
head(data_exportaciones,10)
data_exportaciones %>% data.table()

1.3 Unir las tablas

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

data_importaciones %>% full_join(
  data_exportaciones,
  by = c("pais", "sac", "anio", "mes"),
  suffix = c("_importaciones", "_exportaciones")
) -> data_comercio_exterior
data_comercio_exterior %>% replace_na(
  list(
    valor_cif = 0,
    valor_fob = 0,
    kilogramos_importaciones = 0,
    kilogramos_exportaciones = 0
  )
) %>%
  arrange(pais, anio, mes, sac) -> data_comercio_exterior 
data_comercio_exterior %>% head() %>%
  kable(caption = "Base de Comercio Exterior 2018-2023",
        align = "c") %>%
  add_footnote(label = "Elaboración propia con base en datos del BCR",
               notation = "symbol") %>%
  kable_styling()
Base de Comercio Exterior 2018-2023
pais sac anio mes valor_cif kilogramos_importaciones valor_fob kilogramos_exportaciones
Afganistan 0806200000 2018 6 6448.43 1463.92 0 0
Afganistan 6104220000 2018 10 3153.37 2407.61 0 0
Afganistan 6104620000 2018 10 946.01 722.28 0 0
Afganistan 6105100000 2018 10 9405.39 7181.03 0 0
Afganistan 6106100000 2018 10 1353.32 1725.55 0 0
Afganistan 6405900000 2018 10 2260.03 1725.55 0 0
* Elaboración propia con base en datos del BCR

1.4 Guardar la tabla

save(data_comercio_exterior,file = "C:/Users/HEIDY ALVAREZ/Desktop/Proyecto MAE.RData")

2 Realice la actualización de la tabla anterior con toda la información disponible para 2023

library(dplyr)
library(tidyr)
library(kableExtra)
setwd("C:/Users/HEIDY ALVAREZ/Desktop/Proyecto MAE/Importaciones actualizacion/")

archivos_importar <- list.files()

lista_importacion <- lapply(
  archivos_importar,
  FUN = read_delim,
  delim = "|",
  col_names = c("pais",
                "sac",
                "anio",
                "mes",
                "valor_cif",
                "kilogramos"),
  col_types = cols(
    pais = "c",
    sac = "c",
    anio = "d",
    mes = "d",
    valor_cif = "d",
    kilogramos = "d"
  ),
  skip = 1
)

data_importaciones_update <- bind_rows(lista_importacion)


setwd("C:/Users/HEIDY ALVAREZ/Desktop/Proyecto MAE/Exportaciones actualizacion/")

archivos_importar <- list.files()

lista_importacion <- lapply(
  archivos_importar,
  FUN = read_delim,
  delim = "|",
  col_names = c("pais",
                "sac",
                "anio",
                "mes",
                "valor_fob",
                "kilogramos"),
  col_types = cols(
    pais = "c",
    sac = "c",
    anio = "d",
    mes = "d",
    valor_cif = "d",
    kilogramos = "d"
  ),
  skip = 1
)

data_exportaciones_update <- bind_rows(lista_importacion)



data_importaciones_update %>% full_join(
  data_exportaciones_update,
  by = c("pais", "sac", "anio", "mes"),
  suffix = c("_importaciones", "_exportaciones")
) -> data_comercio_exterior_update


data_comercio_exterior_update %>% replace_na(
  list(
    valor_cif = 0,
    valor_fob = 0,
    kilogramos_importaciones = 0,
    kilogramos_exportaciones = 0
  )
) %>%
  arrange(pais, anio, mes, sac) -> data_comercio_exterior_update


data_comercio_exterior %>% bind_rows(data_comercio_exterior_update) %>%  arrange(pais, anio, mes, sac) ->
  data_comercio_exterior


save(data_comercio_exterior, file = "C:/Users/HEIDY ALVAREZ/Desktop/Proyecto MAE/Exportaciones actualizacion.RData")


data_comercio_exterior %>% filter(anio %in% c(2021, 2022, 2023)) %>% head() %>%
  kable(caption = "Base de Comercio Exterior 2018-2023",
        align = "c") %>%
  add_footnote(label = "Elaboración propia con base en datos del BCR",
               notation = "symbol") %>%  kable_styling()
Base de Comercio Exterior 2018-2023
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
* Elaboración propia con base en datos del BCR

3 Agregue la información estandarizada de los países (nombres iso de los países y regiones, etc). Muestre un head de 10 casos.

options (scipen = 999)
library(dplyr)
library(readxl)

load("C:/Users/HEIDY ALVAREZ/Desktop/Proyecto MAE/Exportaciones actualizacion.RData")


nombre_archivo <- "C:/Users/HEIDY ALVAREZ/Downloads/master_paises_iso.xlsx"
nombres_iso_paises <- read_excel(nombre_archivo)
data_comercio_exterior %>%
  left_join(nombres_iso_paises,
            by = c("pais" = "nom_pais_esp")) -> data_comercio_exterior


anios_ranking<-2018:2023
data_comercio_exterior %>% 
  filter(anio %in% anios_ranking) ->data_ranking
head(data_comercio_exterior,10)

4 Obtenga un ranking, anual, de los 5 principales socios comerciales de El Salvador, para el periodo 2018-2022.

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
library(tidyr)
insumo_reporte %>% 
pivot_wider(names_from = rank,values_from = data)->mi_tabla
print(mi_tabla)
## # A tibble: 6 × 6
##    anio `1`      `2`       `3`       `4`      `5`     
##   <dbl> <chr>    <chr>     <chr>     <chr>    <chr>   
## 1  2018 NA|47.09 HND|15.34 GTM|14.36 NIC|6.87 CRI|4.39
## 2  2019 NA|44.41 GTM|15.92 HND|15.9  NIC|6.66 CRI|4.46
## 3  2020 NA|43.91 GTM|16.42 HND|15.56 NIC|7.1  CRI|4.49
## 4  2021 NA|43.83 GTM|16.78 HND|16.49 NIC|7.2  CRI|4.11
## 5  2022 NA|41.17 GTM|17.05 HND|16.65 NIC|6.98 CRI|4.27
## 6  2023 NA|38.49 GTM|17.93 HND|15.94 NIC|7.39 CRI|4.62
library(kableExtra)
mi_tabla %>% 
  kable(caption = paste("Top 5 Socios comerciales, periodo",min(anios_ranking), "-",max(anios_ranking))) %>% 
  add_footnote(label = "elaboracion propia con base en datos del BCR") %>% 
  kable_styling()
Top 5 Socios comerciales, periodo 2018 - 2023
anio 1 2 3 4 5
2018 NA|47.09 HND|15.34 GTM|14.36 NIC|6.87 CRI|4.39
2019 NA|44.41 GTM|15.92 HND|15.9 NIC|6.66 CRI|4.46
2020 NA|43.91 GTM|16.42 HND|15.56 NIC|7.1 CRI|4.49
2021 NA|43.83 GTM|16.78 HND|16.49 NIC|7.2 CRI|4.11
2022 NA|41.17 GTM|17.05 HND|16.65 NIC|6.98 CRI|4.27
2023 NA|38.49 GTM|17.93 HND|15.94 NIC|7.39 CRI|4.62
a elaboracion propia con base en datos del BCR