class: center, middle, inverse, title-slide .title[ # Semillero R ] .subtitle[ ## Sesión 2: Tidyverse y bibliotecas auxiliares ] .institute[ ### Universidad Nacional - Universidad de Antioquia ] .date[ ### 2021-03-29 ] --- # Objetos básicos en R <center> <img src = "https://es.r4ds.hadley.nz/diagrams_w_text_as_path/es/data-structures-overview.svg" width = 600 /> </center> --- # Objetos estructurados en R <center> <img src = "https://rstudio-education.github.io/hopr/images/hopr_0306.png" width = 700 /> </center> --- background-image: url(https://www.tidyverse.org/images/tidyverse-default.png) background-position: 50% 50% class: center, bottom, inverse # --- class: center, middle # Proceso de análisis de datos <center> <img src = "https://es.r4ds.hadley.nz/diagrams_w_text_as_path/es/data-science-wrangle.svg" /> </center> --- # Procesos comunes en manipulación de datos ## 1. Coercionar una variable ## 2. Seleccionar una o más variables ## 3. Filtrar filas basado en alguna condición ## 4. Editar nombres de variables ## 5. Calcular métricas estadísticas --- # R "convencional" vs `tidyverse` ## Ejemplo 1: coercionar una variable -- .pull-left[ ### R "convencional" ```r ejemplo <- iris class(ejemplo$Species) ``` ``` ## [1] "factor" ``` ```r ejemplo$Species <- as.character(ejemplo$Species) class(ejemplo$Species) ``` ``` ## [1] "character" ``` ] -- .pull-right[ ### `tidyverse` ```r library(tidyverse) ejemplo <- ejemplo %>% mutate(Species = as.character(Species)) ``` ] --- # R "convencional" vs `tidyverse` ## Ejemplo 2: seleccionar una o más variables -- .pull-left[ ### R "convencional" ```r # Primera forma forma1 <- iris[, c(2, 3)] # Segunda forma forma2 <- iris[, c("Sepal.Width", "Petal.Length")] # Tercera forma forma3 <- subset(iris, select = c(Sepal.Width, Petal.Length)) ``` ] -- .pull-right[ ### `tidyverse` ```r library(tidyverse) forma_tidy <- iris %>% select(Sepal.Width, Petal.Length) ``` ] --- # R "convencional" vs `tidyverse` ## Ejemplo 3: filtrar observaciones -- .pull-left[ ### R "convencional" ```r # Filtro 1 (mayor que) filtro1 <- iris[iris$Sepal.Length > 6, ] # Filtro 2 (igual a) filtro2 <- iris[iris$Species == "versicolor", ] # Filtro 3 filtro3 <- subset(iris, Sepal.Length > 6) ``` ] -- .pull-right[ ### `tidyverse` ```r library(tidyverse) filtro_tidy1 <- iris %>% filter(Sepal.Length > 6) filtro_tidy2 <- iris %>% filter(Sepal.Length > 6 & Species == "versicolor") ``` ] --- # R "convencional" vs `tidyverse` ## Ejemplo 4: editar nombres de variables -- .pull-left[ ### R "convencional" ```r # Forma 1 names(iris) <- c("v1", "v2", "v3", "v4", "v5") # Forma 2 setNames(ejemplo, nm = c("v1", "v2", "v3", "v4", "v5")) ``` ] -- .pull-right[ ### `tidyverse` ```r # Todos los nombres library(tidyverse) nombres_tidy1 <- iris %>% set_names(c("v1", "v2", "v3", "v4", "v5")) # Nombres individuales nombres_tidy2 <- iris %>% rename(nuevo_nombre = Sepal.Length) ``` ] --- # R "convencional" vs `tidyverse` ## Ejemplo 5: calcular la media de una variable -- .pull-left[ ### R "convencional" ```r mean(iris$Sepal.Length) ``` ``` ## [1] 5.843333 ``` ] -- .pull-right[ ### `tidyverse` ```r iris %>% pull(Sepal.Length) %>% mean() ``` ``` ## [1] 5.843333 ``` ] --- class: inverse, center, middle # Bibliotecas [`tidyverse`](https://www.tidyverse.org/) <center> <img src = "https://magrittr.tidyverse.org/logo.png" width = 150 /> </center> .pull-left[ .pull-left[ <center> <img src = "https://readr.tidyverse.org/logo.png" width = 85 /> </center> <center> <img src = "https://readxl.tidyverse.org/logo.png" width = 85 /> </center> <center> <img src = "https://d33wubrfki0l68.cloudfront.net/c477d7eb7fdf2c3d75637cfe19ff4a4d0a107bcf/017d0/css/images/hex/tibble.png" width = 85 /> </center> ] .pull-right[ <center> <img src = "https://tidyr.tidyverse.org/logo.png" width = 85 /> </center> <center> <img src = "https://cran.r-project.org/web/packages/janitor/readme/man/figures/logo_small.png" width = 85 /> </center> <center> <img src = "https://d33wubrfki0l68.cloudfront.net/621a9c8c5d7b47c4b6d72e8f01f28d14310e8370/193fc/css/images/hex/dplyr.png" width = 85 /> </center> ] ] .pull-right[ .pull-left[ <center> <img src = "https://d33wubrfki0l68.cloudfront.net/45fd04ad9cdb2159fea08d07dbc11e742d68e4e3/df327/css/images/hex/stringr.png" width = 85 /> </center> <center> <img src = "https://d33wubrfki0l68.cloudfront.net/412a6f14518ab633a94221dda7e16cf22e43a763/91620/css/images/hex/forcats.png" width = 85 /> </center> <center> <img src = "https://bobaekang.github.io/icjia-r-workshop/images/lubridate.png" width = 85 /> </center> ] .pull-right[ <center> <img src = "https://hms.tidyverse.org/logo.png" width = 85 /> </center> <center> <img src = "https://docs.ropensci.org/skimr/reference/figures/logo.png" width = 85 /> </center> <center> <img src = "https://ggplot2.tidyverse.org/logo.png" width = 85 /> </center> ] ] --- class: inverse, center, middle # Ejemplo [AlimenTro](https://alimentro.agrosavia.co/Home/Index?ReturnUrl=%2f) <iframe width="800" height="500" src="https://www.youtube.com/embed/dvuhqn4ekl0" frameborder="0" allowfullscreen></iframe> --- class: inverse, center, middle <center> <img src = "img/alimentro.png" width = 500/> </center> ## Descargar datos [aquí](/Actividades/2021_01/data/Alimentos_del_tr_pico_para_alimentaci_n_animal_-_AlimenTro.csv) ## Consultar [datos abiertos AlimenTro](https://www.datos.gov.co/Agricultura-y-Desarrollo-Rural/Alimentos-del-tr-pico-para-alimentaci-n-animal-Ali/6arb-d547) --- class: inverse, center, middle # Pasos a seguir ### 1. Limpiar nombres ### 2. Transformación de variables ### 3. Seleccionar variables de interés ### 4. Obtener métricas descriptivas ### 5. Construir visualizaciones --- class: inverse, center, middle # Ejemplos datos desordenados > *“Todos los set de datos ordenados se parecen unos a otros, pero cada set de datos desordenado lo es a su manera”* — Hadley Wickham <center> <img src="img/no_tidy.jpeg"; height= 300/> </center> --- class: inverse, center, middle # Ejemplo 1: Homicidios en Quindío ## Descargar datos [aquí](/Actividades/2021_01/data/Tasas_de_homicidios_seg_n_municipios_por_cien_mil_habitantes._A_os_1990_-_2017.csv) ## Consultar [datos abiertos *homicidios departamento del Quindío 1990-2017*](https://www.datos.gov.co/Estad-sticas-Nacionales/Tasas-de-homicidios-seg-n-municipios-por-cien-mil-/7n7b-qavj) --- class: inverse, center, middle # Ejemplo 2: Caudal río Chinchiná 2016 ## Descargar datos [aquí](/Actividades/2021_01/data/Caudal_Rio_Chinchina_2016.csv) ## Consultar [datos abiertos *caudal río Chinchiná 2016*](https://www.datos.gov.co/Ambiente-y-Desarrollo-Sostenible/Caudal-Rio-Chinchina-2016/ng69-pr5i) --- class: inverse, center, middle # Ejemplo 3: Accidentes de tránsito Florencia 2011 ## Descargar datos [aquí](/Actividades/2021_01/data/Accidentes_de_transporte__tasas_de_lesiones_por_100.000_habitantes__seg_n_edad_y_sexo__Colombia__2011.csv) ## Consultar [datos abiertos *accidentes de tránsito Florencia, Caquetá - 2011*](https://www.datos.gov.co/Estad-sticas-Nacionales/Accidentes-de-transporte-tasas-de-lesiones-por-100/6fck-pt5v) --- class: inverse, center, middle # Ejemplo 4: Raíces, bulbos y tubérculos en el Valle del Cauca ## Descargar datos [aquí](/Actividades/2021_01/data/data_cultivos_valle.zip) ### Consultar [datos abiertos *superficie sembrada raíces, bulbos y tubérculos*](https://www.datos.gov.co/Agricultura-y-Desarrollo-Rural/Superficie-Sembrada-con-Ra-ces-Bulbos-y-Tub-rculos/h7vs-gf6t) ### Consultar [datos abiertos *superficie cosechada raíces, bulbos y tubérculos*](https://www.datos.gov.co/Agricultura-y-Desarrollo-Rural/Superficie-Cosechada-con-Ra-ces-Bulbos-y-Tub-rculo/2uev-8vbt) ### Consultar [datos abiertos *producción raíces, bulbos y tubérculos*](https://www.datos.gov.co/Agricultura-y-Desarrollo-Rural/Producci-n-en-Toneladas-por-Hect-reas-de-Ra-ces-Bu/hg7h-itmt) ### Consultar [datos abiertos *rendimiento raíces, bulbos y tubérculos*](https://www.datos.gov.co/Agricultura-y-Desarrollo-Rural/Rendimiento-en-toneladas-por-hect-reas-en-Cultivos/239z-ikj9) --- class: inverse, center, middle # [*R para Ciencia de Datos*](https://es.r4ds.hadley.nz/) <center> <img src = "https://images-na.ssl-images-amazon.com/images/I/51U8H-yROqL._SX331_BO1,204,203,200_.jpg" width = 300 /> </center> --- class: inverse, center, middle # [*Libro Vivo de Ciencia de Datos*](https://librovivodecienciadedatos.ai/) <center> <img src = "https://librovivodecienciadedatos.ai/introduction/libro_vivo_de_ciencia_de_datos_cover.png" width = 300 /> </center> --- class: inverse, center, middle # <center> <img src = "img/gracias.gif" width = 600 /> </center>