2006

casen_2006 <- readRDS(file = "casen_2006_c.rds")

ab <- casen_2006
      
a <- ab$YAUTHAJ
b <- ab$COMUNA
c<- ab$codigo
d<- ab$E1
e <- ab$T4
f <- ab$SEXO

promedios_grupales <-aggregate(a, by=list(b, c, d, e, f), FUN = mean , na.rm = TRUE)

promedios_grupales$anio <- "2006"
      
names(promedios_grupales)[1] <- "Comuna"     
names(promedios_grupales)[2] <- "Código"
names(promedios_grupales)[3] <- "Sabe leer?"
names(promedios_grupales)[4] <- "Etnia"
names(promedios_grupales)[5] <- "Sexo"
names(promedios_grupales)[6] <- "Promedio del Ingreso autónomo del hogar"
names(promedios_grupales)[7] <- "Año"  

head(promedios_grupales,5)
##          Comuna Código Sabe leer?  Etnia   Sexo
## 1       Iquique   1101         Sí Aymara Hombre
## 2 Alto Hospicio   1107         Sí Aymara Hombre
## 3  Pozo Almonte   1401         Sí Aymara Hombre
## 4        Camiña   1402         Sí Aymara Hombre
## 5      Colchane   1403         Sí Aymara Hombre
##   Promedio del Ingreso autónomo del hogar  Año
## 1                                763708.7 2006
## 2                                370986.9 2006
## 3                                770483.4 2006
## 4                                303475.7 2006
## 5                                315219.3 2006
#write_xlsx(promedios_grupales, "Promedio_ingresos_autonomoshogar_sexo_etnia_alfa_2006.xlsx")

2009

casen_2009 <- readRDS(file = "casen_2009_c.rds")

ab <- casen_2009
      
a <- ab$YAUTHAJ
b <- ab$COMUNA
c<- ab$codigo
d<- ab$E1
e <- ab$T5
f <- ab$SEXO

promedios_grupales <-aggregate(a, by=list(b, c, d, e, f), FUN = mean , na.rm = TRUE)

promedios_grupales$anio <- "2009"
      
names(promedios_grupales)[1] <- "Comuna"     
names(promedios_grupales)[2] <- "Código"
names(promedios_grupales)[3] <- "Sabe leer?"
names(promedios_grupales)[4] <- "Etnia"
names(promedios_grupales)[5] <- "Sexo"
names(promedios_grupales)[6] <- "Promedio del Ingreso autónomo del hogar"
names(promedios_grupales)[7] <- "Año"  

# promedios_grupales
head(promedios_grupales,5)
##          Comuna Código Sabe leer?  Etnia   Sexo
## 1       Iquique   1101         Sí Aymara Hombre
## 2 Alto Hospicio   1107         Sí Aymara Hombre
## 3  Pozo Almonte   1401         Sí Aymara Hombre
## 4        Camiña   1402         Sí Aymara Hombre
## 5      Colchane   1403         Sí Aymara Hombre
##   Promedio del Ingreso autónomo del hogar  Año
## 1                                530118.5 2009
## 2                                594287.0 2009
## 3                                692591.6 2009
## 4                                389665.5 2009
## 5                                234659.2 2009
#write_xlsx(promedios_grupales, "Promedio_ingresos_autonomoshogar_sexo_etnia_alfa_2009.xlsx")

2011

casen_2011 <- readRDS(file = "casen_2011_c.rds")

ab <- casen_2011
      
a <- ab$yauthaj
b <- ab$comuna
c<- ab$codigo
d<- ab$e1
e <- ab$r6
f <- ab$sexo

promedios_grupales <-aggregate(a, by=list(b, c, d, e, f), FUN = mean , na.rm = TRUE)

promedios_grupales$anio <- "2011"
      
names(promedios_grupales)[1] <- "Comuna"     
names(promedios_grupales)[2] <- "Código"
names(promedios_grupales)[3] <- "Sabe leer?"
names(promedios_grupales)[4] <- "Etnia"
names(promedios_grupales)[5] <- "Sexo"
names(promedios_grupales)[6] <- "Promedio del Ingreso autónomo del hogar"
names(promedios_grupales)[7] <- "Año"  

head(promedios_grupales,5)
##          Comuna Código        Sabe leer?  Etnia   Sexo
## 1       Iquique   1101 Sí, lee y escribe Aymara Hombre
## 2 Alto Hospicio   1107 Sí, lee y escribe Aymara Hombre
## 3  Pozo Almonte   1401 Sí, lee y escribe Aymara Hombre
## 4        Camiña   1402 Sí, lee y escribe Aymara Hombre
## 5         Huara   1404 Sí, lee y escribe Aymara Hombre
##   Promedio del Ingreso autónomo del hogar  Año
## 1                               1072166.3 2011
## 2                                835478.6 2011
## 3                                497637.6 2011
## 4                                783404.0 2011
## 5                                471656.3 2011
#write_xlsx(promedios_grupales, "Promedio_ingresos_autonomoshogar_sexo_etnia_alfa_2011.xlsx")

2013

casen_2013 <- readRDS(file = "casen_2013_c.rds")

ab <- casen_2013
      
a <- ab$yautcorh
b <- ab$comuna
c<- ab$codigo
d<- ab$e1
e <- ab$r6
f <- ab$sexo

promedios_grupales <-aggregate(a, by=list(b, c, d, e, f), FUN = mean , na.rm = TRUE)

promedios_grupales$anio <- "2013"
      
names(promedios_grupales)[1] <- "Comuna"     
names(promedios_grupales)[2] <- "Código"
names(promedios_grupales)[3] <- "Sabe leer?"
names(promedios_grupales)[4] <- "Etnia"
names(promedios_grupales)[5] <- "Sexo"
names(promedios_grupales)[6] <- "Promedio del Ingreso autónomo del hogar"
names(promedios_grupales)[7] <- "Año"  

head(promedios_grupales,5)
##          Comuna Código        Sabe leer?  Etnia   Sexo
## 1       Iquique   1101 Sí, lee y escribe Aymara Hombre
## 2 Alto Hospicio   1107 Sí, lee y escribe Aymara Hombre
## 3  Pozo Almonte   1401 Sí, lee y escribe Aymara Hombre
## 4        Camiña   1402 Sí, lee y escribe Aymara Hombre
## 5         Huara   1404 Sí, lee y escribe Aymara Hombre
##   Promedio del Ingreso autónomo del hogar  Año
## 1                                884683.1 2013
## 2                                779242.4 2013
## 3                                447613.1 2013
## 4                                262037.1 2013
## 5                                356562.5 2013
#write_xlsx(promedios_grupales, "Promedio_ingresos_autonomoshogar_sexo_etnia_alfa_2013.xlsx")

2015

casen_2015 <- readRDS(file = "casen_2015_c.rds")

ab <- casen_2015
      
a <- ab$yautcorh
b <- ab$comuna
c<- ab$codigo
d<- ab$e1
e <- ab$r3
f <- ab$sexo

promedios_grupales <-aggregate(a, by=list(b, c, d, e, f), FUN = mean , na.rm = TRUE)

promedios_grupales$anio <- "2015"
      
names(promedios_grupales)[1] <- "Comuna"     
names(promedios_grupales)[2] <- "Código"
names(promedios_grupales)[3] <- "Sabe leer?"
names(promedios_grupales)[4] <- "Etnia"
names(promedios_grupales)[5] <- "Sexo"
names(promedios_grupales)[6] <- "Promedio del Ingreso autónomo del hogar"
names(promedios_grupales)[7] <- "Año"  

head(promedios_grupales,5)
##          Comuna Código        Sabe leer?  Etnia   Sexo
## 1       Iquique   1101 Sí, lee y escribe Aimara Hombre
## 2 Alto Hospicio   1107 Sí, lee y escribe Aimara Hombre
## 3  Pozo Almonte   1401 Sí, lee y escribe Aimara Hombre
## 4        Camiña   1402 Sí, lee y escribe Aimara Hombre
## 5         Huara   1404 Sí, lee y escribe Aimara Hombre
##   Promedio del Ingreso autónomo del hogar  Año
## 1                               1207984.9 2015
## 2                                672255.1 2015
## 3                                595851.7 2015
## 4                                366330.6 2015
## 5                                451726.8 2015
#write_xlsx(promedios_grupales, "Promedio_ingresos_autonomoshogar_sexo_etnia_alfa_2015.xlsx")

2017

casen_2017 <- readRDS(file = "casen_2017_c.rds")

ab <- casen_2017
      
a <- ab$yautcorh
b <- ab$comuna
c<- ab$codigo
d<- ab$e1
e <- ab$r3
f <- ab$sexo

promedios_grupales <-aggregate(a, by=list(b, c, d, e, f), FUN = mean , na.rm = TRUE)

promedios_grupales$anio <- "2017"
      
names(promedios_grupales)[1] <- "Comuna"     
names(promedios_grupales)[2] <- "Código"
names(promedios_grupales)[3] <- "Sabe leer?"
names(promedios_grupales)[4] <- "Etnia"
names(promedios_grupales)[5] <- "Sexo"
names(promedios_grupales)[6] <- "Promedio del Ingreso autónomo del hogar"
names(promedios_grupales)[7] <- "Año"  

head(promedios_grupales,5)
##          Comuna Código        Sabe leer?  Etnia   Sexo
## 1       Iquique   1101 Sí, lee y escribe Aimara Hombre
## 2 Alto Hospicio   1107 Sí, lee y escribe Aimara Hombre
## 3  Pozo Almonte   1401 Sí, lee y escribe Aimara Hombre
## 4        Camiña   1402 Sí, lee y escribe Aimara Hombre
## 5         Huara   1404 Sí, lee y escribe Aimara Hombre
##   Promedio del Ingreso autónomo del hogar  Año
## 1                               1256584.8 2017
## 2                                790403.7 2017
## 3                                489883.4 2017
## 4                                670195.6 2017
## 5                                688632.5 2017
write_xlsx(promedios_grupales, "Promedio_ingresos_autonomoshogar_sexo_etnia_alfa_2017.xlsx")