Created
September 21, 2018 14:41
-
-
Save ferblape/5f9fd53cddd7c5f261b934d4ab8cf94f to your computer and use it in GitHub Desktop.
Script to clean "Contratos menores dataset" from https://datos.madrid.es/portal/site/egob/menuitem.c05c1f754a33a9fbe4b2e4b284f1a5a0/?vgnextoid=c331ef300ebe5610VgnVCM1000001d4a900aRCRD&vgnextchannel=374512b9ace9f310VgnVCM100000171f5a0aRCRD&vgnextfmt=default
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sys.setlocale("LC_ALL", "en_US.UTF-8") | |
Sys.setenv(LANG="en_US.UTF-8") | |
Sys.setenv(LC_CTYPE="UTF-8") | |
library(tidyverse) | |
library(lubridate) | |
parse_amount <- function(x) { | |
x <- gsub("[^0-9\\,\\.]", "", x) | |
x <- gsub("\\.", "", x) | |
x <- gsub("\\,", ".", x) | |
return(as.double(x)) | |
} | |
classify_contract <- function(c) { | |
others <- c("Contrato administrativo especial", "Contrato de consultoría y asistencia", "Contrato de gestión de servicios público", "Contrato privado") | |
return(case_when( | |
c == "Contrato de obras" ~ "Obras", | |
c == "OBRAS" ~ "Obras", | |
c == "Contrato de servicios" ~ "Servicios", | |
c == "SERVICIOS" ~ "Servicios", | |
c == "Contrato de suministros" ~ "Suministro", | |
c == "SUMINISTRO" ~ "Suministro", | |
c == "OTROS" ~ "Otros", | |
c %in% others ~ "Otros" | |
)) | |
} | |
classify_freelance <- function(nif) { | |
return(grepl("^[0-9]",nif)) | |
} | |
# 2018 data | |
df_2018 <- read_delim("ContratosMenores2018_utf8.csv", ';') %>% | |
as_tibble() %>% | |
select("NUMERO EXPEDIENTE", "SECCION ", "OBJETO DEL CONTRATO", "N.I.F", "CONTRATISTA", "IMPORTE", "TIPO DE CONTRATO", "FCH.COMUNIC.REG") %>% | |
mutate(fecha_contabilidad = dmy(`FCH.COMUNIC.REG`), importe = parse_amount(`IMPORTE`), anyo = year(fecha_contabilidad)) %>% | |
select(-`IMPORTE`, -`FCH.COMUNIC.REG`) %>% | |
rename(n_expediente = `NUMERO EXPEDIENTE`, seccion = `SECCION `, titulo = `OBJETO DEL CONTRATO`, nif = `N.I.F`, contratista = `CONTRATISTA`, tipo_contrato = `TIPO DE CONTRATO`) %>% | |
mutate(tipo_contrato = classify_contract(tipo_contrato), freelance = classify_freelance(nif)) | |
# 2017 data | |
df_2017 <- read_delim("ContratosMenores2017_utf8.csv", ';') %>% | |
as_tibble() %>% | |
select("Nº expediente", "Descripción", "Título del expediente", "NIF", "Contratista", " Importe", "Tipo de expediente", "Fe.contab.") %>% | |
mutate(fecha_contabilidad = dmy(`Fe.contab.`), importe = parse_amount(` Importe`), anyo = year(fecha_contabilidad)) %>% | |
select(-` Importe`, -`Fe.contab.`) %>% | |
rename(n_expediente = `Nº expediente`, seccion = `Descripción`, titulo = `Título del expediente`, nif = `NIF`, contratista = `Contratista`, tipo_contrato = `Tipo de expediente`) %>% | |
mutate(tipo_contrato = classify_contract(tipo_contrato), freelance = classify_freelance(nif)) | |
# 2016 | |
df_2016 <- read_delim("ContratosMenores2016_utf8.csv", ';', col_types="icccccccccc") %>% | |
as_tibble() %>% | |
select("Nº expediente", "Descripción", "Título del expediente", "NIF", "Tercero", " Importe", "Tipo de expediente", "Fe/contab/") %>% | |
mutate(fecha_contabilidad = dmy(`Fe/contab/`), importe = parse_amount(` Importe`), anyo = year(fecha_contabilidad)) %>% | |
select(-` Importe`, -`Fe/contab/`) %>% | |
rename(n_expediente = `Nº expediente`, seccion = `Descripción`, titulo = `Título del expediente`, nif = `NIF`, contratista = `Tercero`, tipo_contrato = `Tipo de expediente`) %>% | |
mutate(tipo_contrato = classify_contract(tipo_contrato), freelance = classify_freelance(nif)) | |
# 2015 | |
df_2015 <- read_delim("ContratosMenores2015_utf8.csv", ';') %>% | |
as_tibble() %>% | |
select("Nº exped. Adm.", "Descripción", "Título del expediente", "NIF", "Tercero", "Importe", "T. expediente", "Fe.contab.") %>% | |
mutate(fecha_contabilidad = dmy(`Fe.contab.`), importe = parse_amount(`Importe`), anyo = year(fecha_contabilidad)) %>% | |
select(-`Importe`, -`Fe.contab.`) %>% | |
rename(n_expediente = `Nº exped. Adm.`, seccion = `Descripción`, titulo = `Título del expediente`, nif = `NIF`, contratista = `Tercero`, tipo_contrato = `T. expediente`) %>% | |
mutate(tipo_contrato = classify_contract(tipo_contrato), freelance = classify_freelance(nif)) | |
df <- df_2018 %>% bind_rows(df_2017) %>% bind_rows(df_2016) %>% bind_rows(df_2015) %>% filter(!is.na(nif)) | |
write_csv(df, "contratos_menores_madrid.csv") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment