Skip to content
Snippets Groups Projects
Commit c0ddd482 authored by Daniel.Kalioudjoglou's avatar Daniel.Kalioudjoglou
Browse files

suite script chargement ptz

parent 5e00afa8
Branches
No related tags found
No related merge requests found
library(readr)
library(tidyr)
library(dplyr)
library(COGiter)
library(stringr)
library(lubridate)
millesime = 2023
# ma_region = "52"
# Départements de ma région
# mes_departements <- list_dep_in_reg(ma-region)
# path <- paste("/nfs/data/partage-PTZ-EPTZ/PTZ-EPTZ/BASE_PTZ_DHUP_",millesime,"_DREAL.csv")
BASE_PTZ_DHUP<- read_delim(paste0("/nfs/data/partage-PTZ-EPTZ/PTZ-EPTZ/BASE_PTZ_DHUP_",millesime,"_DREAL.csv"),
delim = ";", escape_double = FALSE, trim_ws = TRUE)
ptz <- BASE_PTZ_DHUP %>%
base <- BASE_PTZ_DHUP %>%
# cpfl = code postal , cins = code insee, cdco = code commune déclaré
# lcom = libéllé commune, timm = type logement (1 = individuel, 2 = collectif)
# tope = type d'opération :
......@@ -17,20 +25,60 @@ ptz <- BASE_PTZ_DHUP %>%
# 5 : Transformation de locaux non destinés à l’habitation en logement
# 6 : Levée d’option en location-accession, pour le 1er locataire
# 7 : Acquisition d’un logement avec travaux de remise à neuf
select (cpfl,cins,cdco,lcom,timm,tope) %>%
select (cpfl,cins,cdco,lcom,timm,tope,an) %>%
mutate(type_logt = case_when(
timm == 1 ~ "individuel",
timm == 2 ~ "collectif",
TRUE ~ "non_renseigne"),
TRUE ~ "type_non_renseigne"),
etat_logt = case_when(
tope %in% c(1,2,3) ~ "neuf",
tope %in% c(4,5,6,7) ~ "ancien",
TRUE ~ "non_renseigne")
TRUE ~ "etat_non_renseigne")
) %>%
rename(code_insee = cins) %>%
group_by(code_insee,type_logt,etat_logt) %>%
summarise(nb_logt = length(code_insee)) %>%
ungroup()
arrange(code_insee)
ptz_detail <- base %>%
group_by(an,code_insee,type_logt,etat_logt) %>%
summarise(valeur = length(code_insee)) %>%
ungroup() %>%
mutate(variable = paste0("ptz-",type_logt,"-",etat_logt)) %>%
select(-type_logt,-etat_logt)
ptz_total <- base %>%
group_by(an,code_insee) %>%
summarise(valeur = length(code_insee)) %>%
ungroup() %>%
mutate(variable="ptz-total")
ptz_type <- base %>%
group_by(an,code_insee,type_logt) %>%
summarise(valeur = length(code_insee)) %>%
ungroup() %>%
mutate(variable = paste0("ptz-",type_logt)) %>%
select(-type_logt)
ptz_etat <- base %>%
group_by(an,code_insee,etat_logt) %>%
summarise(valeur = length(code_insee)) %>%
ungroup() %>%
mutate(variable = paste0("ptz-",etat_logt)) %>%
select(-etat_logt)
# recapitulatif
ptz <- bind_rows(ptz_detail,ptz_etat,ptz_type,ptz_total) %>%
select(depcom=code_insee,an,variable,valeur) %>%
complete(an,depcom,variable) %>%
mutate_all(funs(ifelse(is.na(.),0,.))) %>%
mutate(date=make_date(an,12,31))%>%
select(-an) %>%
pivot_wider(names_from = variable,values_from = valeur)
# rm(ptz_detail,ptz_etat,ptz_type,ptz_total)
# cogification
cogifiee_ptz<-cogifier(ptz %>% rename(DEPCOM=depcom))%>%
mutate_if(is.factor,as.character)
#controle
nb_lgts <- ptz %>% pull(nb_logt) %>% sum()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment