-
Notifications
You must be signed in to change notification settings - Fork 0
/
10_Poblacion.R
31 lines (23 loc) · 971 Bytes
/
10_Poblacion.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# Poblacion total
#Connections
library(RMySQL)
library(tidyverse)
con <- src_mysql(dbname = "censo2012",
host = Sys.getenv("MYSQL_HOST"),
user = Sys.getenv("MYSQL_USER"),
password = Sys.getenv("MYSQL_PASS")
)
#Start querying
# Generating ID_W
geoid <- tbl(con,"idgeo") %>%
select(FOLIO,NVIV,COM,DTO,AREA,ZONA,MZ,LOCALIDAD,ENTIDAD,SECTOR) %>%
collect(n=Inf) %>%
mutate(ID_W=paste(COM,sprintf("%02d", DTO),AREA,ifelse(AREA==1, sprintf("%03d",ZONA), ifelse(AREA==2, sprintf("%03d",LOCALIDAD), 0)),ifelse(AREA==1, sprintf("%03d",MZ), ifelse(AREA==2, sprintf("%03d",ENTIDAD), 0)),sep="")) %>%
select(FOLIO,NVIV,ID_W)
# Calculating population by manzana
numberpeople <-tbl(con,"poblacion") %>%
select(FOLIO,NVIV) %>% collect(n=Inf) %>%
left_join(geoid, by=c("FOLIO"="FOLIO","NVIV"="NVIV")) %>%
group_by(ID_W) %>%
summarize(value = n())
writeRDS(numberpeople,"poblacion.RDS")