Skip to content

Instantly share code, notes, and snippets.

@fernandobarbalho
Created March 31, 2026 12:37
Show Gist options
  • Select an option

  • Save fernandobarbalho/13af7a0a04e78710fecaf781e2565259 to your computer and use it in GitHub Desktop.

Select an option

Save fernandobarbalho/13af7a0a04e78710fecaf781e2565259 to your computer and use it in GitHub Desktop.
Script para extrair dados da ANFAVEZ
library(tidyverse)
library(readxl)
endereco_base<- "https://anfavea.com.br/docs/siteautoveiculos"
nomme_sheet<- "III. Licenciamento Combustível"
emplacamento_por_combustivel<-
purrr::map_dfr(2012:2026, function(ano){
print(ano)
endereco_ano <- paste0(endereco_base, ano,".xlsx" )
endereco_destino<- paste0("data/dados_anfavea",ano,".xlsx")
download.file(endereco_ano, destfile = endereco_destino, mode= "wb")
dados_anfavea <- read_excel(endereco_destino,
sheet = 4,
skip = 5)
dados_anfavea<- dados_anfavea[1:8,-c(1,15)]
names(dados_anfavea)[1] <- "combustivel"
dados_anfavea %>%
filter(!is.na(combustivel))%>%
pivot_longer(2:13, names_to = "data", values_to = "valor") %>%
mutate(data = as.Date(paste0("01-", data,"-", ano), format = "%d-%b-%Y"))%>%
mutate(valor = as.numeric(valor))
})
saveRDS(emplacamento_por_combustivel, "emplacamento_por_combustivel.rds")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment