Skip to content

Instantly share code, notes, and snippets.

@fernandobarbalho
Last active August 2, 2023 12:47
Show Gist options
  • Save fernandobarbalho/3f1d1e94235eb251eb2d1933b93fdba8 to your computer and use it in GitHub Desktop.
Save fernandobarbalho/3f1d1e94235eb251eb2d1933b93fdba8 to your computer and use it in GitHub Desktop.
FAz downoload da tabela com dados de população, área e densidade demográfica de municípios. Em seguida faz tratamentos na tabela. Retorna um dataframe
gera_tabela_ibge_municipios<- function(){
# Load required libraries
library(httr)
library(jsonlite)
library(janitor)
library(tidyverse)
# API endpoint URL
api_url <- "https://apisidra.ibge.gov.br/values/t/4714/n6/all/v/all/p/all/d/v614%202"
data_list <- fromJSON(api_url)
names_df<- data_list[1,]
data_list <- data_list[-1,]
names(data_list) <- names_df
data_list <- janitor::clean_names(data_list)
ibge_municipios<-
data_list %>%
mutate(valor =as.numeric(valor)) %>%
select(municipio_codigo,
municipio,
variavel,
valor,
ano) %>%
pivot_wider(id_cols = c(municipio_codigo, municipio), names_from = variavel, values_from = valor) %>%
separate(municipio, into = c("municipio", "uf"), sep = " - ")
ibge_municipios<- janitor::clean_names(ibge_municipios)
ibge_municipios
}
#Exemplo de uso
ibge2022<-
gera_tabela_ibge_municipios()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment