Librerias

library(readr)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(data.table)
## 
## Attaching package: 'data.table'
## The following objects are masked from 'package:dplyr':
## 
##     between, first, last
library(tidyr)
library(kableExtra)
## 
## Attaching package: 'kableExtra'
## The following object is masked from 'package:dplyr':
## 
##     group_rows
library(readxl)
library(knitr)

Parte 1

1.1 Importaciones 2018 - 2020

setwd("C:/Users/eazuc/OneDrive/Escritorio/Datos BC/Importaciones/")
IMPORTACIONES_2018_2020 <- list.files()

Lista_IMPORTACIONES <- lapply(IMPORTACIONES_2018_2020, 
                                      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
                                      ) 
## Warning: The following named parsers don't match the column names: kilogramos
## The following named parsers don't match the column names: kilogramos
## The following named parsers don't match the column names: kilogramos
## The following named parsers don't match the column names: kilogramos
## The following named parsers don't match the column names: kilogramos
## The following named parsers don't match the column names: kilogramos
importaciones_men_2020<-bind_rows(Lista_IMPORTACIONES)
head(importaciones_men_2020)
## # A tibble: 6 × 6
##   pais   sac         anio   mes valor_cif Kilogramos
##   <chr>  <chr>      <dbl> <dbl>     <dbl>      <dbl>
## 1 Canada 0404900000  2018     2   68040      18810  
## 2 Canada 0404900000  2018     3   92726.     25529. 
## 3 Canada 0404900000  2018     4   92546.     25525  
## 4 Canada 0404900000  2018     6   91531      25525  
## 5 Canada 0405100000  2018     6      26.6       20  
## 6 Canada 0511100000  2018     1   16365.        23.6

1.2 Exportaciones 2018 - 2020

setwd("C:/Users/eazuc/OneDrive/Escritorio/Datos BC/Exportaciones/")
EXPORTACIONES_2018_2020 <- list.files()

Lista_EXPORTACIONES <- lapply(EXPORTACIONES_2018_2020, 
                                      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
                                      ) 
## Warning: The following named parsers don't match the column names: valor_cif, kilogramos
## The following named parsers don't match the column names: valor_cif, kilogramos
## The following named parsers don't match the column names: valor_cif, kilogramos
## The following named parsers don't match the column names: valor_cif, kilogramos
## The following named parsers don't match the column names: valor_cif, kilogramos
## The following named parsers don't match the column names: valor_cif, kilogramos
exportaciones_men_2020<-bind_rows(Lista_EXPORTACIONES)
head(exportaciones_men_2020)
## # A tibble: 6 × 6
##   pais   sac         anio   mes valor_fob Kilogramos
##   <chr>  <chr>      <dbl> <dbl>     <dbl>      <dbl>
## 1 Canada 0106900000  2018     2     4667.       15  
## 2 Canada 0106900000  2018     3     4237.       14  
## 3 Canada 0106900000  2018     4     3918        13.5
## 4 Canada 0106900000  2018     5     3155.       10  
## 5 Canada 0106900000  2018     6     3097.        9  
## 6 Canada 0305720000  2018     2     2702.      280

1.3 Tabla base de datos del comercio exterior (2018 - 2020)

importaciones_men_2020 %>% full_join(exportaciones_men_2020, 
                                          by = c("pais",
                                                 "sac",
                                                 "anio",
                                                 "mes"),
                                          suffix = c("_importaciones"
                                                     ,"_exportaciones")) -> df_comercio_exterior
df_comercio_exterior <- df_comercio_exterior %>% replace_na(
  list(
    valor_cif=0,
    valor_fob=0,
    Kilogramos_importaciones=0,
    Kilogramos_exportaciones=0
  )
) %>% arrange(pais,sac,mes,anio)

df_comercio_exterior %>% head(n = 10) %>%
  kable(caption = "Base de Comercio Exterior 2018-2020",
        align = "c") %>%
  add_footnote(label = "Elaboración propia con base en datos del BCR",
               notation = "symbol") %>%  
  kable_styling() %>%
  row_spec(0, bold = TRUE, color = "black")
Base de Comercio Exterior 2018-2020
pais sac anio mes valor_cif Kilogramos_importaciones valor_fob Kilogramos_exportaciones
Afganistan 0806200000 2018 6 6448.43 1463.92 0.00 0
Afganistan 0910200000 2019 10 1821.00 34.90 0.00 0
Afganistan 3004909100 2019 9 0.00 0.00 382.13 19
Afganistan 3924909000 2020 9 26.89 8.31 0.00 0
Afganistan 4202190000 2020 8 18.88 0.68 0.00 0
Afganistan 4202990000 2020 8 7.55 0.27 0.00 0
Afganistan 4901990000 2020 9 67.22 8.31 0.00 0
Afganistan 6103109000 2020 8 75.52 2.73 0.00 0
Afganistan 6104220000 2018 10 3153.37 2407.61 0.00 0
Afganistan 6104620000 2018 10 946.01 722.28 0.00 0
* Elaboración propia con base en datos del BCR

Parte 2

2.1 Actualizacion Importaciones hasta 2023

setwd("C:/Users/eazuc/OneDrive/Escritorio/Datos BC/Importaciones 2023/")
IMPORTACIONES_2018_2023 <- list.files()

Lista_IMPORTACIONES_2023 <- lapply(IMPORTACIONES_2018_2023, 
                                      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
                                      ) 
## Warning: The following named parsers don't match the column names: kilogramos
## The following named parsers don't match the column names: kilogramos
## The following named parsers don't match the column names: kilogramos
## The following named parsers don't match the column names: kilogramos
## The following named parsers don't match the column names: kilogramos
## The following named parsers don't match the column names: kilogramos
## The following named parsers don't match the column names: kilogramos
## The following named parsers don't match the column names: kilogramos
## The following named parsers don't match the column names: kilogramos
## The following named parsers don't match the column names: kilogramos
## The following named parsers don't match the column names: kilogramos
## The following named parsers don't match the column names: kilogramos
importaciones_men_2023<-bind_rows(Lista_IMPORTACIONES_2023)
head(importaciones_men_2023)
## # A tibble: 6 × 6
##   pais   sac         anio   mes valor_cif Kilogramos
##   <chr>  <chr>      <dbl> <dbl>     <dbl>      <dbl>
## 1 Canada 0404900000  2018     2   68040      18810  
## 2 Canada 0404900000  2018     3   92726.     25529. 
## 3 Canada 0404900000  2018     4   92546.     25525  
## 4 Canada 0404900000  2018     6   91531      25525  
## 5 Canada 0405100000  2018     6      26.6       20  
## 6 Canada 0511100000  2018     1   16365.        23.6

2.2 Actualizacion EXportaciones hasta 2023

setwd("C:/Users/eazuc/OneDrive/Escritorio/Datos BC/Exportaciones 2023/")
EXPORTACIONES_2018_2023 <- list.files()

Lista_EXPORTACIONES_2023 <- lapply(EXPORTACIONES_2018_2023, 
                                      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
                                      ) 
## Warning: The following named parsers don't match the column names: valor_cif, kilogramos
## The following named parsers don't match the column names: valor_cif, kilogramos
## The following named parsers don't match the column names: valor_cif, kilogramos
## The following named parsers don't match the column names: valor_cif, kilogramos
## The following named parsers don't match the column names: valor_cif, kilogramos
## The following named parsers don't match the column names: valor_cif, kilogramos
## The following named parsers don't match the column names: valor_cif, kilogramos
## The following named parsers don't match the column names: valor_cif, kilogramos
## The following named parsers don't match the column names: valor_cif, kilogramos
## The following named parsers don't match the column names: valor_cif, kilogramos
## The following named parsers don't match the column names: valor_cif, kilogramos
## The following named parsers don't match the column names: valor_cif, kilogramos
Exportaciones_men_2023<-bind_rows(Lista_EXPORTACIONES_2023)
head(Exportaciones_men_2023)
## # A tibble: 6 × 6
##   pais   sac         anio   mes valor_fob Kilogramos
##   <chr>  <chr>      <dbl> <dbl>     <dbl>      <dbl>
## 1 Canada 0106900000  2018     2     4667.       15  
## 2 Canada 0106900000  2018     3     4237.       14  
## 3 Canada 0106900000  2018     4     3918        13.5
## 4 Canada 0106900000  2018     5     3155.       10  
## 5 Canada 0106900000  2018     6     3097.        9  
## 6 Canada 0305720000  2018     2     2702.      280

2.3 Tabla de comercio exterior de 2018 a 2023

importaciones_men_2023 %>% full_join(Exportaciones_men_2023, 
                                          by = c("pais",
                                                 "sac",
                                                 "anio",
                                                 "mes"),
                                          suffix = c("_importaciones"
                                                     ,"_exportaciones")) -> df_comercio_exterior23

df_comercio_exterior23 <- df_comercio_exterior23 %>% replace_na(
  list(
    valor_cif=0,
    valor_fob=0,
    Kilogramos_importaciones=0,
    Kilogramos_exportaciones=0
  )
) %>% arrange(pais,sac,mes,anio)

df_comercio_exterior23 %>% head(n = 10) %>%
  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() %>%
  row_spec(0, bold = TRUE, color = "black")
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.00 0
Afganistan 0910200000 2019 10 1821.00 34.90 0.00 0
Afganistan 3004909100 2019 9 0.00 0.00 382.13 19
Afganistan 3924909000 2020 9 26.89 8.31 0.00 0
Afganistan 4202190000 2020 8 18.88 0.68 0.00 0
Afganistan 4202990000 2020 8 7.55 0.27 0.00 0
Afganistan 4901990000 2020 9 67.22 8.31 0.00 0
Afganistan 5702310000 2022 6 913.74 22.02 0.00 0
Afganistan 5702502000 2022 6 566.35 11.36 0.00 0
Afganistan 6103109000 2020 8 75.52 2.73 0.00 0
* Elaboración propia con base en datos del BCR

Parte 3

3.1 Cargar base de datos Iso, region, etc.

df_iso <- read_excel ("C:/Users/eazuc/OneDrive/Escritorio/Datos BC/codigo Iso/master_paises_iso.xlsx")
## New names:
## • `codigo_pais` -> `codigo_pais...5`
## • `codigo_pais` -> `codigo_pais...6`
## • `` -> `...13`
df_ISO <- df_iso %>% select(pais, iso_3, cod_region, cod_region_intermedia)

3.2 agregar el df_ISO a nuestro df_comercio_exterior

df_comercio_exterior23 <- df_comercio_exterior23 %>% 
  left_join(df_ISO, by = "pais")

3.3 Tabla actualizada del comercio exterior incluido el codigo ISO de cada pais y su region.

head(df_comercio_exterior23 , 10)
## # A tibble: 10 × 11
##    pais       sac         anio   mes valor_cif Kilogramos_importacio…¹ valor_fob
##    <chr>      <chr>      <dbl> <dbl>     <dbl>                   <dbl>     <dbl>
##  1 Afganistan 0806200000  2018     6   6448.                   1464.          0 
##  2 Afganistan 0910200000  2019    10   1821                      34.9         0 
##  3 Afganistan 3004909100  2019     9      0                       0         382.
##  4 Afganistan 3924909000  2020     9     26.9                     8.31        0 
##  5 Afganistan 4202190000  2020     8     18.9                     0.68        0 
##  6 Afganistan 4202990000  2020     8      7.55                    0.27        0 
##  7 Afganistan 4901990000  2020     9     67.2                     8.31        0 
##  8 Afganistan 5702310000  2022     6    914.                     22.0         0 
##  9 Afganistan 5702502000  2022     6    566.                     11.4         0 
## 10 Afganistan 6103109000  2020     8     75.5                     2.73        0 
## # ℹ abbreviated name: ¹​Kilogramos_importaciones
## # ℹ 4 more variables: Kilogramos_exportaciones <dbl>, iso_3 <chr>,
## #   cod_region <chr>, cod_region_intermedia <chr>

Parte 4

años_ranking <- 2018:2023 
  df_comercio_exterior23 %>%
    filter(anio %in% años_ranking) ->df_ranking

4.1 Vista de la tabla de los top 5 de exportadores a El Salvador (2018 - 2023).

tabla_ranking <- df_ranking %>% 
  group_by(anio, iso_3) %>% 
  summarise(total= sum(valor_fob)) %>% 
  mutate(porcentaje = 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, "|", porcentaje, sep = "")) %>% 
  select(anio, data, rank) %>%
  rename(año = anio)
## `summarise()` has grouped output by 'anio'. You can override using the
## `.groups` argument.
tabla_especial <- tabla_ranking %>%
  pivot_wider(names_from = rank, values_from = data)

tabla_especial <- tabla_especial %>%
 mutate(across(where(is.numeric), ~format(., big.mark = ",", 
                                          decimal.mark = ".", 
                                          nsmall = 2)))

tabla_especial %>% 
  kable(caption = paste("Top", 5, "de Exportaciones periodo", 
                        min(años_ranking), max(años_ranking))) %>%
  row_spec(1, bold = TRUE, color = "blue") %>%
  add_footnote(label = "Elaboracion propia con base en datos del BCR")
Top 5 de Exportaciones periodo 2018 2023
año 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