#carga de librerias

library(kableExtra)
## Warning in !is.null(rmarkdown::metadata$output) && rmarkdown::metadata$output
## %in% : 'length(x) = 2 > 1' in coercion to 'logical(1)'
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following object is masked from 'package:kableExtra':
## 
##     group_rows
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(readxl)

#carga de datos

nombres_iso_paises <- read_excel("nombres_iso_paises.xlsx")
load("C:/Users/pc/Downloads/data_comercio_exterior_actualizada.RData")
source(file = "C:/Users/pc/Downloads/funciones_comercio_exterior.R")

#PREPARACION DE TABLA A UTILIZAR

data_comercio_exterior %>% left_join(nombres_iso_paises,by=c("pais"="nom_pais_esp"))->data_comercio_exterior

#EJERCICIO NO. 1 Hacer una funcion que se llame totales_pais_anios que use como argumentos el codigo del pais iso de tres caracteres, para el elegir el socio comercial de el salvador y permita obtener las exportaciones y las importaciones para un periodo especifico

library(dplyr)
totales_pais_anios<-function(codigo,periodo){
  Exp_Imp_Total<-data_comercio_exterior %>% 
                  group_by(iso_3.x, anio) %>% 
                  summarise(Exp= sum(valor_fob)/1e6, 
                            Imp= sum(valor_cif)/1e6)
  
  Exp_Imp_Total %>% filter(codigo== iso_3.x , anio %in% periodo)
}

#EJERCICIO 2

tabla_HND<-totales_pais_anios("HND",2017:2020)
colnames(tabla_HND)<-c("Pais", "anio", "Exp_HND", "Imp_HND") 

tabla_HND %>% mutate(Exp_Netas_HND=Exp_HND-Imp_HND)->tabla_HND

tabla_GTM<-totales_pais_anios("GTM", 2017:2020)
colnames(tabla_GTM)<-c("Pais", "anio", "Exp_GTM", "Imp_GTM")
tabla_GTM %>% mutate(Exp_Netas_GTM=Exp_GTM-Imp_GTM)->tabla_GTM

tabla_HND %>% left_join(tabla_GTM, by= c("anio"="anio")) ->mi_tabla

mi_tabla %>% select("anio","Exp_HND", "Imp_HND","Exp_Netas_HND","Exp_GTM", "Imp_GTM","Exp_Netas_GTM")
## # A tibble: 4 × 7
##    anio Exp_HND Imp_HND Exp_Netas_HND Exp_GTM Imp_GTM Exp_Netas_GTM
##   <dbl>   <dbl>   <dbl>         <dbl>   <dbl>   <dbl>         <dbl>
## 1  2017   1594.   1167.          427.   1584.   2092.         -507.
## 2  2018   1811.   1462.          349.   1696.   2309.         -613.
## 3  2019   1877.   1618.          259.   1881.   2533.         -653.
## 4  2020   1553.   1334.          219.   1697.   2424.         -727.
tabla_HND<-totales_pais_anios("HND",2017:2020)
colnames(tabla_HND)<-c("Pais", "anio", "Exp_HND", "Imp_HND") 

tabla_HND %>% mutate(ratio_HND=Exp_HND/Imp_HND)->tabla_HND

tabla_GTM<-totales_pais_anios("GTM", 2017:2020)
colnames(tabla_GTM)<-c("Pais", "anio", "Exp_GTM", "Imp_GTM")
tabla_GTM %>% mutate(ratio_GTM=Exp_GTM/Imp_GTM)->tabla_GTM

tabla_HND %>% left_join(tabla_GTM, by= c("anio"="anio")) ->mi_tabla

mi_tabla %>% select("anio","Exp_HND", "Imp_HND","ratio_HND","Exp_GTM", "Imp_GTM","ratio_GTM")
## # A tibble: 4 × 7
##    anio Exp_HND Imp_HND ratio_HND Exp_GTM Imp_GTM ratio_GTM
##   <dbl>   <dbl>   <dbl>     <dbl>   <dbl>   <dbl>     <dbl>
## 1  2017   1594.   1167.      1.37   1584.   2092.     0.757
## 2  2018   1811.   1462.      1.24   1696.   2309.     0.735
## 3  2019   1877.   1618.      1.16   1881.   2533.     0.742
## 4  2020   1553.   1334.      1.16   1697.   2424.     0.700