library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.5
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ ggplot2   3.5.2     ✔ tibble    3.3.0
## ✔ lubridate 1.9.4     ✔ tidyr     1.3.1
## ✔ purrr     1.1.0     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(ggplot2)
library(eph)

eph_2023_t4 <- get_microdata(
  year = 2023,
  trimester = 4)

summary(eph_2023_t4$CH06)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   -1.00   18.00   34.00   36.23   53.00  102.00
grupos_edad <- eph_2023_t4 %>% 
  mutate(edad_rango = case_when(CH06 %in% c(18:29)~"Joven",
                                CH06 %in% c(30:59)~"Adulto",
                                CH06 >= 60~"Mayor",
                                TRUE ~"Otro"))

unique(grupos_edad$edad_rango)
## [1] "Adulto" "Joven"  "Mayor"  "Otro"
table(grupos_edad$edad_rango)
## 
## Adulto  Joven  Mayor   Otro 
##  17956   8974   8701  11706
grupos_ponderado <- grupos_edad %>% 
  summarise(cant_joven = sum(PONDERA[edad_rango=="Joven"]),
            cant_adulto = sum(PONDERA[edad_rango=="Adulto"]),
            cant_mayor = sum(PONDERA[edad_rango=="Mayor"]),
            cant_otro = sum(PONDERA[edad_rango=="Otro"]))

ggplot(grupos_edad, aes(x=edad_rango, y=PONDERA))+
  geom_col()+
  scale_y_continuous(labels=function(n){format(n, scientific = FALSE)}) +
  labs(title = "Grupos de edad - Ponderado", subtitle = "EPH 2023 - 4° cuatrimestre",
       x = "Edad", y = "Cantidad población")+
  theme_minimal()