Created
July 7, 2020 13:58
-
-
Save sergiospagnuolo/02c56403e455c19d8c6ee25ec1fd582f to your computer and use it in GitHub Desktop.
Baixa arquivos e agrupa dados num mesmo csv sobre compensação de Quarentena a autoridades do Executivo
This file contains hidden or 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
library(glue) | |
library(stringr) | |
library(deflateBR) | |
library(lubridate) | |
# 2017 e 2018 usam .CSV com caixa alta | |
# 2019 e 2019 usam .csv com minúsculas | |
# Não há dados para março de 2019 | |
url <- "http://repositorio.dados.gov.br/segrt/QUARENTENA_{mes}{ano}.CSV" | |
for (mes in c("01","02", "03", "04","05","06","07","08","09","10","11","12")) { | |
for (ano in c(2016)) { | |
download.file(url = glue(url), | |
destfile = glue("QUARENTENA_{mes}{ano}.CSV")) | |
} | |
Sys.sleep(2) | |
} | |
# concatenar todos os csvs | |
csvs <- list.files(full.names=TRUE, pattern = ".CSV") | |
# cria as tabelas a partir das listas | |
agregado <- lapply(csvs,function(i){ | |
tmp <- try(read.csv(i, header = TRUE, sep = ';', fileEncoding = "ISO-8859-1")) | |
if (!inherits(tmp, 'try-error')) tmp | |
#read.csv(i, header= T, sep =";") | |
}) | |
# cria o arquivao a partir da listona | |
dados <- do.call(rbind.data.frame, agregado) | |
# aplica leve limpeza para mudar notação decimal a fim de facilitar análise | |
dados$valor <- as.numeric(gsub("\\,", ".", dados$Valor.da.remuneração)) | |
dados$data <- as.Date(parse_date_time(dados$Ano.Mês.da.remuneração, "Ym")) | |
dados$valor_ajustado <- deflate(dados$valor, dados$data, "05/2020", "ipca") | |
write.csv2(dados, "beneficio_quarentena_2017-2020.csv") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment