Created
January 18, 2022 14:25
-
-
Save schoulten/37fc426ef1ceefcaa96f8e232d1380c7 to your computer and use it in GitHub Desktop.
Importar dados do Bolsa Família (Pagamentos)
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
# Função que baixa e importa dados do Bolsa Família (Pagamentos) do | |
# Portal Transparência/CGU, apontando ano (4 digitos) e mês (2 dígitos) | |
import_pbf_pagmt <- function(year, month){ | |
# URL para acesso aos arquivos | |
base_url <- paste0( | |
"http://www.portaltransparencia.gov.br/", | |
"download-de-dados/bolsa-familia-pagamentos/" | |
) | |
# URL completa com ano e mês do arquivo para download | |
full_url <- paste0(base_url, year, month) | |
# Arquivo e pasta temporária para armazenar download | |
tf <- tempfile() | |
td <- file.path(tempdir() , "unzips") | |
# Baixar arquivo da URL | |
download.file( | |
url = full_url, | |
destfile = tf, | |
mode = "wb" | |
) | |
# Descompactar arquivo baixado | |
unzip(tf, exdir = td) | |
unzipped_file <- list.files( | |
path = td, | |
pattern = "*.csv", | |
full.names = TRUE, | |
recursive = TRUE | |
) | |
# Importar CSV para o R | |
bf_tbl <- data.table::fread( | |
file = unzipped_file, | |
sep = ";", | |
dec = ",", | |
encoding = "Latin-1" | |
) | |
return( | |
janitor::clean_names(bf_tbl) | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment