1. Introduction

Este cuaderno permite identificar los cultivos de mayor relevancia agrícola en las inmediaciones del departamento de Boyacá. Los datos según los cuales serán categorizados fueron extraidos de la página de evaluaciones agropecuarias del instituto UPRA. Las cuales serán montadas en líneas de texto programadas en Rstudio

2. Setup

Lo primero que debemos hacer es instalar las librerías necesarias

library(tidyverse) #Libreria de librerias
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5     v purrr   0.3.4
## v tibble  3.1.6     v dplyr   1.0.8
## v tidyr   1.2.0     v stringr 1.4.0
## v readr   2.1.2     v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(readxl) #Libreria para leer excel

3. Read an excel file with estadisticas agropecuarias municipales

Para saber los nombres de los archivos de excel en nuestro directorio de trabajo

(archivos = list.files(pattern='xls')) 
## [1] "Upra2020.xlsx"
(hojas = readxl::excel_sheets("Upra2020.xlsx")) 
## [1] "Agrícola_SIPRA_AGRONET"       "InventarioBovino"            
## [3] "InventarioPorcino"            "InvBufalosCaprinoOvinoEquino"
## [5] "InvAves"
eva2020 = readxl::read_excel("Upra2020.xlsx", sheet = "Agrícola_SIPRA_AGRONET")
eva2020

4. Subset data relevant to our department

Acá vamos a filtrar la tabla para mostrar solamente la informacion del departamento que nos interesa

Boy2020 = dplyr::filter(eva2020, Departamento == "Boyacá")
Boy2020

5. Clean the filtered data

Notese que hay columnas que no nos interesan, por lo que procedemos a quitarlas

Boy2020.tmp <-  Boy2020 %>% select('Código del Municipio':'Ciclo del cultivo')
Boy2020.tmp
Boy2020.tmp %>% rename(Cod_Mun = 'Código del Municipio', 
                         Grupo = 'Grupo cultivo según especie', 
                         Subgrupo = 'Subgrupo cultivo según especie',
                         AreaSiembra = 'Area Sembrada (ha)',
                         AreaCosecha = 'Area Cosechada (ha)',
                         Produccion = 'Producción (t)',                              
                         Rendimiento = 'Rendimiento (t/ha)', Ciclo='Ciclo del cultivo') ->                                  nBoy2020
nBoy2020
nBoy2020 %>% mutate(AreaSiembra = as.numeric(AreaSiembra),
                       AreaCosecha = as.numeric(AreaCosecha),
                       Produccion = as.numeric(Produccion),
                       Rendimiento = as.numeric(Rendimiento)) -> nBoy2020 
nBoy2020

6. Data processing & analysis

nBoy2020 %>%
  filter(Produccion > 0) %>%
  group_by(Cultivo) %>%
  summarize(total_produccion = sum(Produccion)) %>% 
  arrange(desc(total_produccion))
nBoy2020 %>%
  group_by(Cultivo, Municipio) %>%
  summarize(max_prod = max(Produccion, na.rm = TRUE)) %>%
  slice(which.max(max_prod))  %>%
  arrange(desc(max_prod))
## `summarise()` has grouped output by 'Cultivo'. You can override using the
## `.groups` argument.
nBoy2020 %>%
  group_by(Grupo,Municipio) %>%
  summarize(max_prod = max(Produccion, na.rm = TRUE)) %>%
  slice(which.max(max_prod))  %>%
  arrange(desc(max_prod))
## `summarise()` has grouped output by 'Grupo'. You can override using the
## `.groups` argument.
nBoy2020 %>%
  group_by(Cod_Mun, Municipio, Grupo) %>%
  filter(Grupo=='Tubérculos Y Plátanos') %>% 
  summarize(max_prod = max(Produccion, na.rm = TRUE)) %>%
  arrange(desc(max_prod)) -> Tuberculos2020
## `summarise()` has grouped output by 'Cod_Mun', 'Municipio'. You can override
## using the `.groups` argument.
 Tuberculos2020
nBoy2020 %>%
  group_by(Cod_Mun, Municipio, Grupo) %>%
  filter(Grupo=='Hortalizas') %>% 
  summarize(max_prod = max(Produccion, na.rm = TRUE)) %>%
  arrange(desc(max_prod)) -> Hortalizas2020
## `summarise()` has grouped output by 'Cod_Mun', 'Municipio'. You can override
## using the `.groups` argument.
Hortalizas2020

7. Write selected data to file

Guardamos los archivos en nuestra carpeta

write_csv(Tuberculos2020, "./boy_tuber_2020.csv")
write_csv(Hortalizas2020, "./boy_hort_2020.csv")

8. Reproducibility

sessionInfo()
## R version 4.1.3 (2022-03-10)
## Platform: x86_64-w64-mingw32/x64 (64-bit)
## Running under: Windows 10 x64 (build 19043)
## 
## Matrix products: default
## 
## locale:
## [1] LC_COLLATE=Spanish_Colombia.1252  LC_CTYPE=Spanish_Colombia.1252   
## [3] LC_MONETARY=Spanish_Colombia.1252 LC_NUMERIC=C                     
## [5] LC_TIME=Spanish_Colombia.1252    
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
##  [1] readxl_1.3.1    forcats_0.5.1   stringr_1.4.0   dplyr_1.0.8    
##  [5] purrr_0.3.4     readr_2.1.2     tidyr_1.2.0     tibble_3.1.6   
##  [9] ggplot2_3.3.5   tidyverse_1.3.1
## 
## loaded via a namespace (and not attached):
##  [1] tidyselect_1.1.2 xfun_0.30        bslib_0.3.1      haven_2.4.3     
##  [5] colorspace_2.0-3 vctrs_0.3.8      generics_0.1.2   htmltools_0.5.2 
##  [9] yaml_2.3.5       utf8_1.2.2       rlang_1.0.2      jquerylib_0.1.4 
## [13] pillar_1.7.0     withr_2.5.0      glue_1.6.2       DBI_1.1.2       
## [17] bit64_4.0.5      dbplyr_2.1.1     modelr_0.1.8     lifecycle_1.0.1 
## [21] cellranger_1.1.0 munsell_0.5.0    gtable_0.3.0     rvest_1.0.2     
## [25] evaluate_0.15    knitr_1.37       tzdb_0.2.0       fastmap_1.1.0   
## [29] parallel_4.1.3   fansi_1.0.2      broom_0.7.12     Rcpp_1.0.8      
## [33] backports_1.4.1  scales_1.1.1     vroom_1.5.7      jsonlite_1.8.0  
## [37] bit_4.0.4        fs_1.5.2         hms_1.1.1        digest_0.6.29   
## [41] stringi_1.7.6    grid_4.1.3       cli_3.2.0        tools_4.1.3     
## [45] magrittr_2.0.2   sass_0.4.0       crayon_1.5.0     pkgconfig_2.0.3 
## [49] ellipsis_0.3.2   xml2_1.3.3       reprex_2.0.1     lubridate_1.8.0 
## [53] assertthat_0.2.1 rmarkdown_2.13   httr_1.4.2       rstudioapi_0.13 
## [57] R6_2.5.1         compiler_4.1.3