13. Ejercicio práctico (Usar la población total)

library(readr)
library(dplyr)
## 
## Adjuntando el paquete: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(tidyr)
library(stringr)

1. Filtra personas de entre 18 y 25 años.(Usar Filter)

2. Crea una nueva variable llamada joven (1 si tiene 18–20 años, 0 si no)

poblacion <- read_csv("poblacion.csv")
## Warning: One or more parsing issues, call `problems()` on your data frame for details,
## e.g.:
##   dat <- vroom(...)
##   problems(dat)
## Rows: 309684 Columns: 188
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr  (26): folioviv, numren, madre_id, padre_id, lenguaind, nivel, residenci...
## dbl (129): foliohog, parentesco, sexo, edad, madre_hog, padre_hog, disc_cami...
## lgl  (33): otorg_c, forma_c, noatenc_1, noatenc_6, noatenc_7, noatenc_9, noa...
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
poblacionNueva <- poblacion %>% 
  filter(edad >= 18 & edad <= 25) %>% #Filtrar personas por edades 
  mutate(joven = ifelse(edad >= 18 & edad <= 20, 1, 0)) %>% #Creo variable nueva de joven
select(edad, joven) #Solo para ver que si se haya creado correctamente la nueva variable
poblacionNueva 
## # A tibble: 40,062 × 2
##     edad joven
##    <dbl> <dbl>
##  1    22     0
##  2    18     1
##  3    19     1
##  4    23     0
##  5    19     1
##  6    24     0
##  7    22     0
##  8    25     0
##  9    22     0
## 10    19     1
## # ℹ 40,052 more rows

3. Crear una variable que se llame nivel_educativo

4. Elimina NA en nivel_educativo.

5. Seleccionar solo las variables: edad, joven, nivel_educativo, sexo.

poblacion <- read_csv("poblacion.csv")
## Warning: One or more parsing issues, call `problems()` on your data frame for details,
## e.g.:
##   dat <- vroom(...)
##   problems(dat)
## Rows: 309684 Columns: 188
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr  (26): folioviv, numren, madre_id, padre_id, lenguaind, nivel, residenci...
## dbl (129): foliohog, parentesco, sexo, edad, madre_hog, padre_hog, disc_cami...
## lgl  (33): otorg_c, forma_c, noatenc_1, noatenc_6, noatenc_7, noatenc_9, noa...
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
poblacionNueva <- poblacion %>% 
  filter(edad >= 18 & edad <= 25) %>% 
  mutate(joven = ifelse(edad >= 18 & edad <= 20, 1, 0)) %>% 
  mutate(nivel_educativo=nivel) %>% #creo nueva variable de nivel educativo
  filter(is.na(nivel_educativo) == FALSE) %>% #Esto hace que solo me salgan las que NO son NA en la variable de nivel_educativo. 
  select(edad, joven, nivel_educativo, sexo) #Para ver que si se hizo
poblacionNueva
## # A tibble: 11,696 × 4
##     edad joven nivel_educativo  sexo
##    <dbl> <dbl> <chr>           <dbl>
##  1    18     1 09                  1
##  2    19     1 12                  1
##  3    23     0 12                  2
##  4    19     1 11                  2
##  5    22     0 12                  2
##  6    22     0 12                  2
##  7    19     1 12                  2
##  8    25     0 12                  2
##  9    18     1 09                  1
## 10    20     1 09                  2
## # ℹ 11,686 more rows

6. Crear un resumen con la función summarise donde me digas la cantidad de personas de cada sexo por edad.

resumen <- poblacionNueva %>% #Creo una nueva variable para ver mejor esta última instrucción
  group_by(edad, sexo) %>% #agrupo por sexo y edad
  mutate(sexo = ifelse(sexo == 1, "Hombre", "Mujer")) %>% #cambio el sexo numérico a que diga hombre y mujer. Use mutate porque ya filtramos por sexo y quitamos los NA, entonces no me va a poner los NA como "Mujer".
  summarise(cantidad_personas = length(sexo))
## `summarise()` has regrouped the output.
## ℹ Summaries were computed grouped by edad and sexo.
## ℹ Output is grouped by edad.
## ℹ Use `summarise(.groups = "drop_last")` to silence this message.
## ℹ Use `summarise(.by = c(edad, sexo))` for per-operation grouping
##   (`?dplyr::dplyr_by`) instead.
resumen
## # A tibble: 16 × 3
## # Groups:   edad [8]
##     edad sexo   cantidad_personas
##    <dbl> <chr>              <int>
##  1    18 Hombre              1231
##  2    18 Mujer               1306
##  3    19 Hombre               933
##  4    19 Mujer               1102
##  5    20 Hombre               845
##  6    20 Mujer               1106
##  7    21 Hombre               785
##  8    21 Mujer                921
##  9    22 Hombre               699
## 10    22 Mujer                818
## 11    23 Hombre               455
## 12    23 Mujer                467
## 13    24 Hombre               299
## 14    24 Mujer                272
## 15    25 Hombre               246
## 16    25 Mujer                211